我想通过id选择mysql表中的所有行,并进行两列比较,我的代码为:
$q = "SELECT * FROM `users` WHERE (`id` <> `chat.from_id` AND `id` <> `chat.to_id`)";
但是当我运行它时,我收到了这个错误:
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出现在聊天表中。我的错误在哪里?
答案 0 :(得分:1)
您没有加入聊天表。因此,您无法在查询中使用该表中的列。尝试
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