Reputation: 35
I have 2 tables and want to get all records from table 'klanten' and where the 'automaten' records are matching bedrijfs_id.
this is what i have so far.
$sql = "
SELECT
`automaten`.automaat,
`automaten`.th_nummer,
`klanten`.bedrijf,
`klanten`.bedrijfs_id,
`klanten`.plaats
FROM klanten, automaten
WHERE (`klanten`.bedrijfs_id = `automaten`.bedrijfs_id)
ORDER BY `klanten`.bedrijf ASC
";
resultin in:
+------------+-------------+-----------+--from automaten--+------------+
153509 | Amigo | Smilde | Hot Rocks | 6170155 |
153509 | Amigo | Smilde | Cash Attack | 6410031 |
153512 | Boekanier | Steenwijk | Red Hot Active | 6980051 |
153512 | Boekanier | Steenwijk | Turboplay | 6550298 |
-----------------------------------------------------------------------
bedrijfs_id | bedrijf | plaats | automaat | th_nummer |
+------------+-------------+-----------+------------------+------------+
table klanten bedrijf bedrijfs_id plaats adres telefoon etc.
table automaten bedrijfs_id automaat th_nummer
so I want to get even the records without any automaat field. (which has to be filled in then
Comment: improved formatting, corrected spelling
Mysql table join records from 2 tables but get all records even the ones with empty fields
I have 2 tables and want to get all records from table 'klanten' and where the 'automaten' records are matching bedrijfs_id.
Still no working solution :(
Upvotes: 0
Views: 91
Reputation: 2543
You have to use a left outer join:
http://dev.mysql.com/doc/refman/5.0/en/join.html
Your query will look like:
SELECT
GROUP_CONCAT(`automaten`.automaat),
`automaten`.th_nummer,
`klanten`.bedrijf,
`klanten`.bedrijfs_id,
`klanten`.plaats
FROM klanten
LEFT OUTER JOIN automaten
ON `klanten`.bedrijfs_id = `automaten`.bedrijfs_id
GROUP BY bedrijfs_id,bedrijf,plaats
ORDER BY `klanten`.bedrijf ASC
Upvotes: 0
Reputation: 124
This should be your solution:
$sql = "
SELECT
`automaten`.automaat,
`automaten`.th_nummer,
`klanten`.bedrijf,
`klanten`.bedrijfs_id,
`klanten`.plaats
FROM klanten
LEFT JOIN automaten
ON `klanten`.bedrijfs_id = `automaten`.bedrijfs_id
ORDER BY `klanten`.bedrijf ASC
";
Regards V
Upvotes: 0
Reputation: 77866
I think you meant to do a LEFT JOIN
instead like
SELECT
`automaten`.automaat,
`automaten`.th_nummer,
`klanten`.bedrijf,
`klanten`.bedrijfs_id,
`klanten`.plaats
FROM klanten
LEFT JOIN automaten
ON `klanten`.bedrijfs_id = `automaten`.bedrijfs_id
ORDER BY `klanten`.bedrijf
Upvotes: 2