Skip to content
Advertisement

How do you join multiple foreign keys?

I am creating a blog using PHP and SQL, I am trying to query everything in 1 SQL query so I can bring out all of the comments with the authors and all of the blogs with the authors. Each of them have the user.uuid as a foreign key.

My Blog Table looks like this:

My Blog Comments Table looks like this:

My User Table looks like this:

Here is my current SQL:

The query is working fine, however, it has an unexpected output and looks like this:

I would like the output to have all of the comments for the comment author in 1 row, then each row will be specific to the comment author. IE:

Advertisement

Answer

You’ll need to know how many comments you’ll have every time, but if you do, a pivot may give you what you need. I’m a little confused about your query structure, but I think it may look something like this with a pivot:

This article might help you too: https://docs.microsoft.com/en-us/sql/t-sql/queries/from-using-pivot-and-unpivot?view=sql-server-2017

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement