简体   繁体   English

通过ID通过两列比较从mysql中选择所有行

[英]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: 我想通过ID与两列比较从MySQL表中选择所有行,我的代码是:

$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. chat.from_id存在于聊天表中。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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