对不起,这不是一个高质量的问题,而且我知道我冒着失败的风险,但我正在努力学习。我目前正在进行一个侧面项目,偶然发现了一个我不确定的情况。
我有两个表,需要调用两个共享相同ID号(不同名称)的数据
我现在试着举个例子
表1
| psid | idd |
| 1 | 999 |
| 2 | 42 |
表2
| aid | other |
| 999 | hello world |
| 42 | welcome |
我正在尝试链接idd和aid,同时显示表1中的所有行
实施例
id = 1 / Title:hello world
id = 2 / Title:welcome
我不确定是否可以通过对数据库的单个查询来实现这一点我尝试添加第二个但是它会进入不间断循环。
我没有做太多搜索,因为不确定要搜索什么。
谢谢,抱歉
答案 0 :(得分:2)
select
table1.*,
table2.*
from
table1,
table2
where
table1.idd = table2.aid and
table1.idd = :id
select
t1.*,
t2.*
from
table1 t1
left join
table2 t2
on
t1.idd = t2.aid
where
t1.idd = :id
答案 1 :(得分:1)
SELECT table1.psid, table2.other FROM table1
JOIN table2 ON table1.idd = table2.aid
WHERE table1.idd= 'X' AND table2.aid = 'X'
这应该将两个表连接在一起,并通过为WHERE子句中的每个表指定匹配的id来获取相关信息。
编辑修复了SQL