douyong8801 2012-08-25 09:44
浏览 59

Mysql加入1行多行 - group concat或php?

Consider the following example:

+----------+--------+-------------+----------+
| Person_id| Person | Language_id | Language |
+----------+--------+-------------+----------+
| 1        | Bob    | 5           | English  |
| 1        | Bob    | 3           | Italiano |
| 1        | Bob    | 8           | Deutsch  |
+----------+--------+-------------+----------+

and the query is (not that important, just scripting to show you the table structure):

SELECT pl.Person_id, Person, Language_id, Language FROM people as p
LEFT JOIN people_languages as pl ON p.Person_id = pl.Person_id 
LEFT JOIN languages as l ON pl.language_id = l.language_id
WHERE pl.Person = 1;

So basically, if the tables are constructed in this way, is it better to retrieve all results as shown above and then create a php function that creates a Person Model with languages_id and languages in an array, or using group_concat to retrieve a single row and then explode the languages and languages_id into an array?

By the way, no matter what I do, at the end I'd like to have a Person Model as the following:

class Person { 

    public $person_id; // 1
    public $person; // Bob
    public $language_id; // Array(5, 3, 8)
    public $language; // Array(English, Italiano, Deutsch);

    .
    . // Functions 
    .

}
  • 写回答

3条回答 默认 最新

  • dongzun9958 2012-08-25 09:51
    关注

    Consider using a Dictionary

        class Person { 
    
            public $person_id; // 1
            public $person; // Bob
            //I don't know php but for your idea
            public Dictionary<int,string> languageList; // KeyValuePairs {(5,English),(3,Italiano),(8,Deutsch)}
            .
            . // Functions 
            .
    
        }
    
    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP