[英]MySQL - Selecting data from another table for a WHERE clause
I have the following MySQL query (failed attempt): 我有以下MySQL查询(尝试失败):
SELECT * FROM
(SELECT @rownum:=@rownum+1 rank, id, userID, total
FROM table1 total, (SELECT @rownum:=0) r
WHERE id = 318467989655781389 && mydb.table2.colm = 'false'
ORDER BY total DESC) a
LIMIT 10
However, I get an error saying that mydb.table2.colm
isn't in the field list. 但是,我收到一条错误消息,指出
mydb.table2.colm
不在字段列表中。 How can I get data from another table to use in a WHERE
clause? 如何从另一个表中获取数据以在
WHERE
子句中使用?
You would use a JOIN
. 您将使用
JOIN
。 The query would look something like this: 查询如下所示:
SELECT (@rownum := @rownum + 1) as rank, t1.id, t1.userID, t1.total
FROM table1 t1 JOIN
mydb.table2 t2
ON t1.? = t2.? CROSS JOIN
(SELECT @rownum := 0) params
WHERE t1.id = 318467989655781389 AND t1.colm = 'false'
ORDER BY t1.total DESC
LIMIT 10;
The ?
?
is for the columns used for joining the tables together. 用于将表连接在一起的列。
Notes: 笔记:
FROM
clause. FROM
子句中特定的表。 LIMIT
. LIMIT
不需要子查询。 In some versions of MySQL, a subquery might be necessary for the ORDER BY
, but the query would look different. ORDER BY
可能需要子查询,但查询看起来会有所不同。 AND
operator in SQL is AND
. AND
运算符为AND
。 Although &&
works in MySQL, you might as well use the standard operator. &&
在MySQL中有效,但您最好使用标准运算符。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.