根据where子句从表中选择一列,然后将其与另一个表中的另一列进行匹配


Pick a column from a table depending on a where clause and then match that to another column in another table

我有一个名为$generic的变量,它被传递到加载SQL查询的函数中,这用于与表cms_web_pages中的slug列形成匹配;因此,在这个实例中,让我们说$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"