如何比较同一查询中另一个表的列

时间:2014-12-17 08:49:08

标签: mysql

我有两张表td_exceltd_user。我使用以下查询来显示表td_excel中的记录。它有很多列,但我比较了两列Pickup_Locationregional它完美无缺,但我也希望比较同一查询中另一个表regional的列td_user是有可能吗?

SELECT * 
FROM 
    td_excel 
where 
    `Pickup_Location` LIKE CONCAT(  '%',  `regional` ,  '%' ) OR 
    regional LIKE CONCAT(  '%',  `Pickup_Location` ,  '%' )

3 个答案:

答案 0 :(得分:0)

是的,通过在适当的列上加入td_user和td_excel,就像这样......

SELECT * 
FROM 
    td_excel e
    JOIN td_user u
    ON <join condition>
where 
    `Pickup_Location` LIKE CONCAT(  '%',  `regional` ,  '%' ) OR 
    -- Using the regional column from td_user by qualifying the column name with the table alias
    -- declared above.
    u.regional LIKE CONCAT(  '%',  `Pickup_Location` ,  '%' )

答案 1 :(得分:0)

SELECT * 
FROM 
    td_excel a inner join td_user b on a.id = b.a_id
where 
a.Pickup_Location LIKE CONCAT(  '%',  a.regional ,  '%' ) OR 
a.regional LIKE CONCAT(  '%',  a.Pickup_Location ,  '%' ) OR
b.regional LIKE CONCAT(  '%',  a.regional  ,  '%' )

您忘了告诉td_user的列是什么,所以请填写此答案

答案 2 :(得分:0)

尝试子查询,如:

SELECT * 
FROM td_excel 
WHERE (`Pickup_Location` LIKE CONCAT(  '%',  `regional` ,  '%' ) OR 
      regional LIKE CONCAT(  '%',  `Pickup_Location` ,  '%' )) AND
      regional IN (SELECT regional
                   FROM td_user)