I have two table in my MySQL database:
USERS ('id_user' - 'id_client' -> the same as the id in CLIENTS)
CLIENTS ('id_client' - 'name' etc.)
I want to print all the clients and the respective users. This is my query:
SELECT * FROM clients AS c LEFT JOIN users AS u ON c.id_client = u.id_client
It seems to be ok, but I am having trouble when I try to print the the id_client
from the table clients
. How can I print them using PHP? It seems they are empty... Is my query wrong?
Try,
Either GROUP BY or DISTINCT is needed
SELECT * FROM clients AS c LEFT JOIN users AS u ON c.id_client = u.id_client GROUP BY c.id_client
Simpler than that, just do the following:
SELECT c.*, u.name FROM clients AS c LEFT JOIN users AS u ON c.id_client = u.id_client
This will select everything from table c, and only name from table u. The issue you are having is you are selecting id_client from both tables, which is not necessary, and causes confusion when trying to reference it with php.
SELECT c. ,u. FROM clients c LEFT outer JOIN users u ON c.id_client = u.id_client
Since you have field with the same name in both tables ("id_client"), you have to specify which one to be printed.
Like so:
SELECT u.id_client, c.id_client, ... FROM clients AS c LEFT JOIN users AS u ON c.id_client = u.id_client
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.