tags:

views:

43

answers:

3

EDIT:This did it:

  SELECT DISTINCT profileid 
FROM profilesrelevation 
WHERE profileid NOT IN (
  SELECT profileid FROM profiles
)

I need to get the profileid values that exist in profilesrelevation table but not in profiles table

profiles table have 8107 diffrent "profileid" values while profilesrelevation table has 8380 diffrent "profilesid" values...

profiles.profileid
profilesrelevation.profileid

select * from profiles, profilesrelevation 
where profilesrelevation.profileid != profiles.profileid  

does not work , how?

A: 

You'll be wanting to use a set:

SELECT DISTINCT profileid 
FROM profilesrelevation 
WHERE profileid NOT IN (
  SELECT profileid FROM profiles
)

This selects all rows/columns from table:profilesrelevation where the profileid of the row isn't also in table:profiles :)

Updated: include distinct since it would appear profileid isn't unique in the profilesrelevation table.

Rudu
the query just keep loading, nothing happends, i have a very strong computer, soemthing must be wrong.
HarbinChina
Thats because of the scale of your data, and likely because you don't have indexes on profileid in either table. If you can edit the tables try adding indexes.
Rudu
ABout the index thing, this is not how i usally do thing, i only use the id table for relating tables, but this is just temporary so i did things diffrently and forgot to add index, if it still takes time, maybe i can do two querys, store in array, compare arrays and then store all data in a third array,
HarbinChina
after adding indexes it went well, but i got to many results, let my try again wait
HarbinChina
the query works well now, but i get duplicate results or more of the same profileid, who do i sort them out.
HarbinChina
the mysql_num_rows is over 9000 when it should be 200 or so, because the profileid exsist on many rows like 1, 3 , 7,7,7,7,7, 50,705,705,606
HarbinChina
i sorted them out, look @ opening post
HarbinChina
A: 
SELECT
    profilesrelevation.profileid
FROM
    profilesrelevation
    LEFT JOIN profiles ON profilesrelevation.profileid = profiles.profileid
WHERE
    profiles.profileid IS NULL

(You may wish to use SELECT DISTINCT.)

Hammerite
SELECT DISTINCT help me thanks alot, and you query works
HarbinChina
+3  A: 

Using LEFT JOIN / IS NULL

   SELECT pr.*
     FROM PROFILESREVELATION pr
LEFT JOIN PROFILES ON p.profileid = pr.profileid
    WHERE p.profileid IS NULL

Using NOT EXISTS

SELECT pr.*
  FROM PROFILESREVELATION pr
 WHERE NOT EXISTS(SELECT NULL
                    FROM PROFILES p
                   WHERE p.profileid = pr.profileid)

Using NOT IN

SELECT pr.*
  FROM PROFILESREVELATION pr
 WHERE pr.profileid NOT IN (SELECT p.profileid
                              FROM PROFILES p)

Conclusion

The LEFT JOIN IS NULL is the most efficient on MySQL when the columns compared are not nullable. If the columns compared were nullable, NOT IN and NOT EXISTS are more efficient.

OMG Ponies
Nicely and succinctly explained. I would also add that NOT IN starts to degrade performancew-wise as the subquery returns a greater number of records. Not Exists may be better if the profiles table is very large.
sql_mommy