使用SELECT CONCAT的Mysql结果作为同一查询中的条件

时间:2012-09-26 12:59:51

标签: mysql sql select concat

我正在尝试使用此查询,我需要在比较之前连接字符串以获得正确的结果。查询正在运行,但未找到任何结果。我相信问题是当我尝试比较WHERE子句中的“myname”时。什么是正确的查询?

SELECT * 
FROM (SELECT CONCAT('tm_',name) AS myname FROM room) room, users
WHERE user_name = myname;

2 个答案:

答案 0 :(得分:1)

为什么不

SELECT * FROM users,room where user_name=concat('tm_',name);

答案 1 :(得分:0)

试试这个

SELECT *
FROM 
(
   SELECT CONCAT('tm_',name) AS myname 
   FROM room
) t
WHERE user_name = myname

您可能需要JOIN其他表格users,如此:

SELECT *
FROM 
(
   SELECT CONCAT('tm_',name) AS myname 
   FROM room
) t
INNER JOIN users u ON ... --some join condition
WHERE user_name = myname

SELECT *
FROM 
(
   SELECT CONCAT('tm_',name) AS myname 
   FROM room
) t, users
WHERE t.user_name = myname"