0

I'm trying to create an SQL query that will combine rows where a row sharing an ID is found, but print normally if now. I have a Computer and Harddisk database. The Computer DB has a Id and the Harddisk DB har a Id and a ComputerId for the associated computer. Some computers have more harddisks so I'd like to combine these into the same column.

I've attached a image below explaining the two tables and the desired output: 1

My current query reads: $queryHDDS = "SELECT *,CONCAT(ComputerId) FROM HardDisk GROUP BY Id";

Trouble is it is no combining two harddisks that are associated by a shared ComputerId

Hope someone is able to help me out with this!

Dlaurs20
  • 35
  • 5
  • Please read https://meta.stackoverflow.com/questions/334822/how-do-i-ask-and-answer-homework-questions and add what you have so far (hint join) – P.Salmon Jan 28 '21 at 14:42

1 Answers1

0

you could try using group_concat

    select Computer.ID, group_concat(harddisk.id)
    from Computer 
    inner join harddisk on Computer.id = harddisk.computerID
    group by Computer.ID
ScaisEdge
  • 131,976
  • 10
  • 91
  • 107
  • Thank you for your response. I attempted the following query (yours modified a little to be functional): $join = "SELECT *, group_concat(HardDisk.Id) from Computer inner join HardDisk on Computer.Id = HardDisk.ComputerId group by Computer.Id"; It lists harddisk with computerId 1-10 but there's 2 harddisks that has computer Id 6 and it does not list and join those two together. Do you know what could be causing this? – Dlaurs20 Jan 28 '21 at 15:04
  • @Dlaurs20 .. i'm not in yout data .. i have checkd the img you posted and my query should work correcly (using computer.id) .. but what do you mean with there's 2 harddisks that has computer Id 6 and it does not list and join those two together.?? .. .. try add a poroper data sample as tabular text not as img – ScaisEdge Jan 28 '21 at 15:33
  • I have made a new question with the tables properly setup https://stackoverflow.com/questions/65941945/issue-concatenating-rows-with-duplicates hope you can help! – Dlaurs20 Jan 28 '21 at 17:10