Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select everything, based on distinct USER ID in Oracle

Tags:

sql

oracle

I am trying to select * from an oracle table, but only where user_id are unique.

i tried this:

select distinct user_id from users; -- which worked

i want to display EVERYTHING, so when i put:

select distinct user_id, * from users; -- i get a syntax error

how can i accomplish his?

like image 569
Madam Zu Zu Avatar asked Dec 21 '22 09:12

Madam Zu Zu


2 Answers

select distinct user_id, users.* from users; 
like image 94
schurik Avatar answered Dec 29 '22 00:12

schurik


select * from users where users.primary_key IN 
  (select primary_key FROM users GROUP BY user_id HAVING count(*) = 1)

This will only select records that do not share user_ids with other rows.

like image 27
Johan Avatar answered Dec 29 '22 00:12

Johan