2

I have only a very basic knowledge of SQL, so pls do forgive if this is a stupid Q. I hunted the internet but could not get an answer.

I have three tables:

title

id int <- PK

artist_id int

album_id int

desc varchar

artist

arist_id int <- FK

desc varchar

album

album_id int <- FK

desc varchar


title data

1, 1, 1, "Give me everything"

1, 2, 2, "More"

1, 3, 3, "What makes you beautiful"

artist data

1, "Pitbull"

2, "Usher"

3, "One Direction"

album data

1, "Planet Pit"

2, "More"

3, "Up All Night"

If I want to do a search of one table I do like so:

select * from artist where desc like '%direction%';

My objective is to do a search across three tables. The search SQL query should return rows for "title" table where the search criteria has been met in the "desc" field of any of the three tables using the artist_id and album_id fields (which are the linking keys).

My brain is totally muddled today. If the Q is not clear, pls do ask for clarity.

1

1 Answer 1

3

You need to make a inner join between tables like this:

select t.* from title t
inner join artist ar on ar.artist_id = t.artist_id
inner join album al on al.album_id = t.album_id
where (t.desc like '%direction%' or ar.desc like '%direction%' or al.desc like '%direction%')
Sign up to request clarification or add additional context in comments.

1 Comment

Just back from weekend, so sorry for the delay. Wow! Tried your query and it just worked straight off. Thx aF.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.