查找表2中表1的相关数据

时间:2013-03-11 04:44:16

标签: php mysql

表1将某些数据作为类别

Here is my table1

表2有一些与table1类别相关的数据 Here is my table2

两个表之间的关系是table1中的cat_id和table2中的cat_ids。

我想要的是什么? 我需要显示table1和table2中的所有字段我只需要相关的内容,即cat_id(table1)和cat_ids(table2)中存在的id

我正在使用像select c.* ,cc.* from news_categories cc, news_content c where cc.cat_id = c.cat_ids group by cc.cat_id这样的查询,它只提供来自table1和table2的公共数据。我需要公共数据和table1中的所有类别 谁能帮我?

3 个答案:

答案 0 :(得分:1)

您应该使用JOIN代替。

SELECT t1.*, GROUP_CONCAT(t2.content_id)
FROM table1 t1
LEFT JOIN table2 t2
    ON t2.cat_ids = t1.cat_id
GROUP BY t1.cat_id

答案 1 :(得分:0)

这适用于两个表格的所有字段......

SELECT Table1.*, Table2.*
FROM Table1, Table2
WHERE Table1.cat_id = Table2.cat_ids

这适用于Table2的所有字段和Table2的Content字段......

SELECT Table1.*, Table2.full_content
FROM Table1, Table2
WHERE Table1.cat_id = Table2.cat_ids

答案 2 :(得分:0)

你需要使用外部联接,这种情况下会离开联接..看看Documentation