I am currently working on one table, this table holds for example, customer number, this customer number is found in two other tables that I want to pull details about this number ( name, phone tec.).
So my query displays only customers that have a match in the query of some CODE Number, this is because i`m using INNER Join.
I want to display rows that don’t have match in the other tables. They are in the table that I select from but don’t have the code number. (like an empty row that I want to display)
The next query will be more understandable
$sql="SELECT $tbl_name.*,customers.fname,eventcodes.DISCODE,eventcodes.AREA
FROM $tbl_name
INNER JOIN customers
INNER JOIN eventcodes
ON $tbl_name.Ccode=eventcodes.MokedCcode AND eventcodes.Ccode=customers.Ccode AND $tbl_name.CODE=eventcodes.CODE
ORDER By `id` DESC
LIMIT $start, $limit";
As you can see all the rows I want to display found on $tbl_name, this query work but it display to me only the rows that match the conditions, I want to display all the rows from the table those that don’t have CODE, but I can pull their name for example (fname) from the customers table because I have their code (customer number).
Thanks!
Use
LEFT JOINand watch out the join conditions:Note that: I moved the condition
customers.Ccode = $tablename.Ccodeto the first join, and removed the condition$tbl_name.CODE=eventcodes.CODE, Since the first two joins will insure that condition.