mysql查询加入两个表格like
问题描述:
company_name | macid | expiry_date
---------------------------------------------
abc | 123456789012 | 2017-03-23
qwe |12 | 2018-05-24
asd | 456789123012 | 2019-07-07
abc | 789456123000 | 2017-03-23
abc | 445544444444 | 2018-03-03
abc | 555555555555 | 2017-03-25
company_name | desktop | server
abc 123456789012 555555555555
789456123000
我有上面的两个表,我想所有macid和expiry date是存在于table1和table2。此外,我已将所有macid作为新行和桌面macid和服务器macid存储在不同的列中。我的查询mysql查询加入两个表格like
"select a.macid,a.expity_date from table1 a,table2 b where a.macid like b.desktop or a.macid like %'b.server%'"
但显示结果为空。请帮忙解决。
我想导致
macid | expiry_date
---------------------------------------------
123456789012 | 2017-03-23
789456123000 | 2017-03-23
555555555555 | 2017-03-25
为表2,如果我要搜索MAC_ID他们,我必须使用
"select * from table2 where desktop like '%123456789012%'"
而不%我不能检索记录(百分比)
答
试试这个:
"select a.macid,a.expity_date from table1 a JOIN table2 b ON (a.macid like b.desktop OR a.macid like b.server)"
答
Y你必须做出两个独立的JOIN
然后UINON
的结果。
SELECT macid, expiry_date
FROM table1 JOIN table2
ON table1.macid = table2.desktop
UNION
SELECT macid, expiry_date
FROM table1 JOIN table2
ON table1.macid = table2.server
答
我觉得这只是一个错字,不expity_date
,这是expiry_date
在您的查询,请参阅demo。
select a.macid, a.expiry_date
from table1 a, table2 b
where a.macid like b.desktop or a.macid like b.server
然而,更更好地使用join
不是一个逗号(,)做的事情加盟:
select a.macid, a.expiry_date
from table1 a
join table2 b
on a.macid like b.desktop or a.macid like b.server
还要检查demo这里。
另一件事是,如果desktop
和server
与macid
相同,则只需使用equal(=)即可。
我提到我的所有数据都是以新行的形式存储的。所以我必须用%% – LOKESH
@LOKESH来查询,然后用'like' :) – Blank