简体   繁体   中英

mysql select query with multiple group_concat of the same table

I'm currently working on a small RPG game, but I've came across a small issue.

Unit Table

UnitID | Name
------ | ------
1      | Bob

Equip Table 1

UnitID | EquipTypeID
------ | -----------
1      | 5          
1      | 8          

Equip Table 2

UnitID | EquipTypeID
------ | -----------
1      | 10         
1      | 12         

Say if now I wanted to make an overview of the unit, list all possible equipable types in the slots, say for example like this

UnitID | Name | EquipSlot1 | EquipSlot2
------ | ---- | ---------- | ----------
1      | Bob  | 5, 8       | 10, 12

I know I could use something like group_concat into something like ...

SELECT ut.UnitID, ut.Name,
       group_concat(DISTINCT et1.equipTypeID) as EquipSlot1,
       group_concat(DISTINCT et2.equipTypeID) as EquipSlot2
FROM unitTable as ut
LEFT JOIN equipTable1 as et1
ON ut.UnitID = et1.UnitID
LEFT JOIN equipTable2 as et2
ON ut.UnitID = et2.UnitID

However, what I'm actually thinking is, if it's possible if I could just simply make it like ...

Equip Table (combine the two tables together with additional column to indicate the slots)

UnitID | EquipTypeID | EquipSlot
------ | ----------- | ---------
1      | 5           | 1  
1      | 8           | 1  
1      | 10          | 2  
1      | 12          | 2     

But then how would I achieve the same result if I were to structure this way?

You can use cas, as you only want the number to change based on the table

SELECT ut.UnitID, ut.Name, et1.equipTypeID,et2.equipTypeID
Case 
   when et1.equipTypeID then 1 
   when et2.equipTypeID then 2 
end AS EquipSlot 
FROM unitTable as ut

LEFT JOIN equiptTable1 as et1
ON ut.UnitID = et1.UnitID
LEFT JOIN equiptTable2 as et2
ON ut.UnitID = et2.UnitID

To combine your table use case again

SELECT ut.UnitID, ut.Name, ut.equipTypeID, ut.EquipSlot 
Case  when ut.ut.EquipSlot = 1  then group_concat(ut.equipTypeID) end as EquipSlot1 
Case  when ut.ut.EquipSlot = 2  then group_concat(ut.equipTypeID) end as EquipSlot2   
FROM unitTable as ut

After testing around, I've finally came to find a solution that answers my own question.

So the idea is to use the SELECT on SELECT method to generate the two (or more) tables based on the filter needed as new table in the FROM section, and then just group_concat the result to get the answer that I wanted.

SELECT ut.UnitID, ut.Name, group_concat(DISTINCT et1.equipSlot1), group_concat(DISTINCT et2.equipSlot2)
FROM unitTable as ut, 
(SELECT et.unitID, et.EquipTypeID as equipSlot1 FROM equipTable as et, unitTable as ut WHERE et.unitID = ut.unitID AND et.EquipSlot = 1) as et1,
(SELECT et.unitID, et.EquipTypeID as equipSlot2 FROM equipTable as et, unitTable as ut WHERE et.unitID = ut.unitID AND et.EquipSlot = 2) as et2
WHERE ut.unitID=et1.unitID AND ut.unitID=et2.unitID
GROUP BY ut.unitID

Hopefully someone will run into my same problem and find this helpful.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM