模拟MySQL的多维数组 [英] Simulate multidimensional array on MySQL

查看:251
本文介绍了模拟MySQL的多维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我与这些结构的两个表:

I have two tables with these structure:

文章:article_id的,文本
标签:TAG_ID,article_id的,文本

articles: article_id, text tags: tag_id, article_id, text

我如何选择一个查询所有文章和所有相关的标签?据我所知,MySQL的只能返回一个二维数组,这样我就可以一个解决方案将所有的标签连接成一个文章选择结果的列。但如何?

How can I select all articles AND all associated tags with one query? As I know, MySQL can only return a two-dimensional array, so I can one solution would be to concatenate all tags into a column of an articles select result. But how?

推荐答案

看看的<一个href=\"http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat%20Group_Concat\"相对=nofollow> GROUP_CONCAT MySQL的功能。你可以写类似

Take a look at the Group_Concat MySQL function. You can write something like

Select A.article_id, A.text GROUP_CONCAT(B.Text) As Tags 
From articles A 
Left Outer Join tags B Using (article_id) 
Group By A.article_id, A.Text

由于非理性在他的回答别说,这只能如果你想显示的数据使用,我建议,因为你提到的问题,并置标签。如果你需要的日期的工作,用一个简单的加入成为FABRIK建议。

As Unreason mention in his answer, this should only be used if you want to display the data and I suggested it because you mention concatenating the tags in the questions. If you need to work with the date, use a simple join as fabrik suggested.

这篇关于模拟MySQL的多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆