我有一个名为 $generic
的变量它被传递到加载 SQL
的函数中查询,这用于与 slug
形成匹配表中的列 cms_web_pages
;所以在这个例子中我们可以说 $generic
等于 'bar' 查询然后获取 id
匹配行并使用 id
匹配第二个表中的行 cms_web_page_content
与列 page_id
然后从表 cms_web_page_content
中获取所有数据放置在数组内。
表“cms_web_pages”:
id | title | slug
-----------------
1 | foo | bar
2 | bar | foo
表“cms_web_page_content”:
id | title | content | position | page_id
-----------------------------------------
1 | foo | bar | 1 | 1
2 | bar | foo | 2 | 1
3 | doh | doh | 1 | 2
SQL查询:
$link = db_connect();
$qry = mysqli_query($link,
"SELECT page.*, content.*
FROM cms_web_pages AS page
WHERE page.slug = '".$generic."'
LEFT JOIN cms_web_page_content AS content
ON page.id = content.page_id)
ORDER BY content.position ASC")
or die(mysqli_error($link)
);
$content = array();
提前感谢任何可以提供帮助的人。
最佳答案
Use as :
"SELECT page.*, content.*
FROM cms_web_pages AS page
LEFT JOIN cms_web_page_content AS content
ON (page.id = content.page_id)
WHERE page.slug = '".$generic."'
ORDER BY content.position ASC"
关于php - 根据 where 子句从表中选择一列,然后将其与另一个表中的另一列进行匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16189457/