如何从许多表中加入IN SELECT和LIKE?

时间:2012-11-20 07:47:59

标签: select sql-like

我正在尝试创建一个查询,搜索所有共享类似属性的记录,如下所示:

select * from table_A 
where fullname like in (select firstname from employees where X)

唯一的问题是这只是我的伪代码,我实际上正在对许多表进行排序,因此我目前的真实查询看起来像是:

select * from devices 
where devicename like in (select X from X1 where T in (select T from T1 where Y in     (select Y from Y1 where Z in (select Z from Z1 where AreaName = '74';

我正在尝试使用join命令,但对如何将其应用于此情况非常困惑。

5 个答案:

答案 0 :(得分:1)

尝试:

SELECT d.*
  FROM devices d
 INNER JOIN X1 ON d.devicename like '%'||x1.X||'%'
 INNER JOIN T1 ON x1.T = T1.T
 INNER JOIN Y1 ON T1.Y = Y1.Y
 INNER JOIN Z1 ON Y1.Z = Z1.Z
 WHERE Z1.AreaName = '74'

答案 1 :(得分:1)

你走了......

SELECT D.* 
from Devices D
    inner join X1 X on D.devicename like X.X 
    inner join  T1 T on T.T = X.T
    inner join Y1 Y on Y.Y = T.Y
    inner join Z1 Z on Y.Z = Z1.Z AND Z.Areaname = '74';

答案 2 :(得分:0)

好的,我给你第二个带有Join语法的查询(好吧,我试着):

select * from  Z1 Z1_1 
    JOIN Y1 Y_1 ON Y_1.Z=Z1_1.Z 
    JOIN T1 ON T1.Y=Y_1.Y 
    JOIN X1 X1_2 ON X1_2.T=T1.T 
    JOIN devices  d ON d.devicename=X1_2.X
    where Z1_1.AreaName = '74';

答案 3 :(得分:0)

试试这个:

SELECT d.*
FROM devices d
INNER JOIN X1 ON d.`like` = x1.X
INNER JOIN T1 ON x1.T = T1.T
INNER JOIN Y1 ON T1.Y = Y1.Y
INNER JOIN Z1 ON Y1.Z = Z1.Z
WHERE Z1.AreaName = '74'

答案 4 :(得分:0)

"加入" command运行两个表的叉积,然后仅选择公共列名具有相同值的元组/行。

因此,对于您的查询:

select * from table_A
where fullname like in (select firstname from employees where X)

将是:

select * from table_A
left join employees
on table_A.fullname = employees.firstname
where X