Jump to content


Empty field, query cant find

  • Please log in to reply
1 reply to this topic

#1 Thierry

  • Members
  • PipPipPip
  • Advanced Member
  • 85 posts
  • LocationNetherlands

Posted 28 August 2006 - 10:13 AM

I got a table with some IDs in it, and using (example, field1 is an ID, field2 is a name)

SELECT table1.field1, table1.field2 FROM table1, table2 WHERE table1.field1 = table2.field1 ORDER BY table2.field2, I can order by the name of records in table 2 while I only got the ID in table 1.

That all works fine, but if the table1.field1 (ID) is empty (which it can be, its not the key) then, instead of simply showing no name, it doesnt retrieve the record.
I need it to retrieve the record regardless of its contents, if its empty, it should show no name.
Any ideas?
Have you tried....a reset?

#2 Barand

  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 28 August 2006 - 10:38 AM

use LEFT JOIN to retrieve recs from table2 regardless of match in table1. Where there is a match, table1 value is returned. If no match the table1 value is returned as NULL

SELECT t2.field2, t2.field1, t1.field1
FROM table2 t2 LEFT JOIN table1 t1 ON t2.field1 = t2.field1
ORDER BY t2.field2

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received


|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users