Claire
Claire

Reputation: 3773

Join query problems

I have 2 tables. My tables are as below. I have as script called getTag.php. The idea being a user wants to see all portfolio items I have tagged with say, php. However, I don't just want to do:

SELECT t.portfolio_item_id, p.*
FROM tags t
INNER JOIN portfolio p ON t.portfolio_item_id = p.id
WHERE t.$skillArea = '1'

I want to also get all the columns for the tag table too and join them on. So if php =1 in the tag table, get the rest of the columns in the tag table and their values too along with corresponding portfolio item row in portfolio table

Upvotes: 1

Views: 51

Answers (1)

Darren
Darren

Reputation: 70728

SELECT t.*, p.*
FROM tags t
INNER JOIN portfolio p ON t.portfolio_item_id = p.id
WHERE t.$skillArea = '1'

You could just do SELECT * too but I have included t and p to demonstrate where you went wrong.

  • t is the alias for the tags table
  • p is the alias for the portfolio table

  • t.* brings back all columns from the tags table

  • p.* brings back all columns from the portfolio table

You had t.portfolio_item_id which would only bring back 1 column, where * is a wildcard which is used return all columns in that table.

Upvotes: 2

Related Questions