简体   繁体   中英

Select all rows from mysql by id with two columns comparison

I want to select all rows from mysql table by id with two columns comparison, my code:

$q = "SELECT * FROM `users` WHERE (`id` <> `chat.from_id` AND `id` <> `chat.to_id`)";

But when I run it, I get this error:

Message: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'chat.from_id' in 'where clause'
SELECT * FROM `users` WHERE (`id` <> `chat.from_id` AND `id` <> `chat.to_id`)
File: Z:/home/mag.ru/www/panel/gears/db.php
Line: 107
URL: /chat

chat.from_id is present in the chat table. Where is my error?

You don't join the chat table. So you can't use columns from that table in your query. Try

SELECT u.* 
FROM `users` u
LEFT JOIN chat c on u.id = c.from_id or u.id = c.to_id
WHERE c.id is null

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM