php - mysql查询用like连接两个表

标签 php mysql

company_name | macid          |  expiry_date
---------------------------------------------
abc          |  123456789012  |  2017-03-23
qwe          |  987654321012  |  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

我有上面两个表,我想要表 1 和表 2 中存在的所有 macid 和到期日期。此外,我将所有 macid 存储为新行,并将桌面 macid 和服务器 macid 存储在不同的列中。我的查询

"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 

对于 table2,如果我想搜索 mac_id,我必须使用它们

"select * from table2 where desktop like '%123456789012%'"

没有 %(percentage) 我无法检索记录

最佳答案

我认为这只是一个拼写错误,不是 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在这里。
另一件事是,如果 desktopservermacid 相同,则使用 equal(=) 也可以。

关于php - mysql查询用like连接两个表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42826073/

相关文章:

php - 从 YII 框架中的 3 个表中检索数据

php - 我可以让这个选择的关注者/以下脚本更有组织吗? (PHP/MySQL)

php - 两个项目共享相同的用户帐户

php - 我的mysql语法有错误

php - Symfony Ajax 进度条

PHP:我无法使用脚本连接或将表单数据发送到 mySQL

php - 如何限制已经受限的查询?

php - 合并具有相同绑定(bind)参数的两个查询

php - 仅允许 X 嵌套标签

php - HWID 系统 (vb > php > mysql) 不工作