Php 如何连接下表
我有三张桌子:Php 如何连接下表,php,mysql,Php,Mysql,我有三张桌子: -------- | test | -------- | id | -------- ----------------------- | testconnection | ----------------------- | testid | pharmacyid | ----------------------- ----------------------------------- | testcontent | --
--------
| test |
--------
| id |
--------
-----------------------
| testconnection |
-----------------------
| testid | pharmacyid |
-----------------------
-----------------------------------
| testcontent |
-----------------------------------
| id | testID | title | shortened |
-----------------------------------
我需要将testcontent的
标题
和缩写
与testconnection
链接,以便pharmacyid
字段与我当前的药房循环相链接
下面是它现在的样子:
$sql = mysql_query("SELECT * FROM pharmacies");
while($pharmacy = mysql_fetch_array($sql)) {
and here i'd like to access testcontent's title and shortened for this particular pharmacy
}
应该是这样的:
SELECT cont.title, cont.shortened
FROM testcontent cont
JOIN testconnection conn
ON cont.testId = conn.testId
或
试试看
我希望它能起作用。您使用的是MS SQL Server还是/和MySQL?@jarlh我使用的是MySQL谢谢您的尝试,但这根本不会返回任何结果
SELECT *, cont.title, cont.shortened
FROM pharmacies p
JOIN testconnection conn
ON conn.pharmacyid = p.Id
JOIN testcontent cont
ON cont.testId = conn.testId
SELECT pharmacies.*,
(select title from testcontent where testcontent.testID = testconnection.testid limit 1) as title,
(select shortened from testcontent where testcontent.testID = testconnection.testid limit 1) as shortened
FROM pharmacies
left join testconnection on testconnection.pharmacyid = pharmacies.id
order by pharmacies.id