[英]To get data related to the GROUP_CONCAT in MYSQL PHP
This is the result for Those Two table i got By using this query: 这是通过使用此查询得到的那两个表的结果:
SELECT t1.*, GROUP_CONCAT(t2.content_id)
FROM news_categories t1
LEFT JOIN news_content t2
ON t2.cat_ids = t1.cat_id
GROUP BY t1.cat_id
How can I get the information in table2 from the result(table) GROUP_CONCAT(t2.content_id)
related to the content_id
? 如何从与
content_id
相关的result(table) GROUP_CONCAT(t2.content_id)
获取table2中的信息?
and Retrive those data from table2 Using PHP 并使用PHP从table2中检索这些数据
Well, you got a one-to-many relationship here. 好吧,您在这里有一对多的关系。 Meaning one row in table1 can be related to 0-n rows in table2.
意味着表1中的一行可以与表2中的0-n行相关。
If you you group by the primary key of table1 you limit the result to one result row for each row in table one, by using GROUP_CONCAT(table2_col)
you concatenate all matching rows of the table2_column with a separator which is comma by default. 如果按表1的主键进行分组,则将结果限制为表1中每一行的一个结果行,通过使用
GROUP_CONCAT(table2_col)
您可以将table2_column的所有匹配行连接在一起,默认情况下使用逗号分隔。
Play with the following queries: 玩以下查询:
SELECT
t1.*,
GROUP_CONCAT(t2.content_id) AS content_ids,
GROUP_CONCAT(t2.title) AS content_titles
FROM
news_categories t1
LEFT JOIN news_content t2 ON t2.cat_ids = t1.cat_id
GROUP BY t1.cat_id
Or 要么
SELECT
t1.*,
t2.*
FROM
news_categories t1
LEFT JOIN news_content t2 ON t2.cat_ids = t1.cat_id
I recommend to write out the columns you want, instead of using .* 我建议写出所需的列,而不要使用。*
EDIT 编辑
To separate the concatenated values in php have a look at explode 要分离php中的串联值,请看看explode
You fetch the data as usual eg 您照常获取数据,例如
$result = mysqli_query( $query );
while ( $row = mysqli_fetch_assoc( $result ) ) {
print_r( $row );
}
Have a look at this examples 看看这个例子
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.