0

I'm making a search function in PHP and I have three tables that I wish to join to a single one; the three tables looks as follow:

band
ID | bands
---+----------
 1 | Muse
 2 | Coldplay
 3 | etc.

release
ID | releases
---+----------
 1 | Showbiz
 2 | Origin of Symmentry
 3 | etc.

track
ID | tracks
---+-----------
 1 | Sunburn
 2 | Muscle Museum 
 3 | etc.

I want these tables to be put into this:

discografic
ID | band_id  | release_id  | track_id
---+----------+-------------+---------
 1 | 1        | 1           | 1
 2 | 1        | 1           | 2
 3 | etc.

So that the table with the SQL code looks like this:

discografic
ID | bands    | releases    | tracks
---+----------+-------------+---------
 1 | Muse     | Showbiz     | Sunburn
 2 | Muse     | Showbiz     | Muscle Museum
 3 | etc.

I want to INNER JOIN these tables. I joined one but I can't really figure out how the get the last joined as well.

SELECT * 
FROM band
INNER JOIN discografic 
ON band.id = discografic.band_id

This should probably have its own question; I also want to be able to search this database, but only have the result show up once, and also reference to the band every time. For example, if I search "Showbiz" it will give me "Muse", and only show it once.

Note: This is for testing purposes only, security is none of my concerns.

3 Answers 3

2

Try with this query:

select d.id,b.bands,r.releases,t.tracks from discografic as d INNER JOIN band as b on 
d.band_id=b.id INNER JOIN release as r on d.release_id=r.id INNER JOIN track as t on 
d.track_id=t.id GROUP BY d.id
Sign up to request clarification or add additional context in comments.

Comments

0

Try This query

    Select a.ID,b.bands,c.releases,d.tracks from discografic as a 
    inner join band as b on a.band_id = b.ID
    inner join release as c on a.release_id = c.ID
    inner join track as d on a.track_id = d.ID
    where b.bands = 'Muse'

3 Comments

It's probably Wamp creating problems but shouldn't this work? WHERE b.bands = '%$searchq%' I have made $searchq the variable for anything I enter into the search field.
if you are trying to use % then use a like keywords instead of '=' in where condition (ex:- where b.bands like '%$searchq%'
The query you made worked, and the table looks like it should. I'll try and get my PHP to work with what you suggest. Thanks by the way.
0

Use this query to insert the data like you wanted:

Insert into discograpy
(id,bands,releases,tracks)
SELECT band.ID,bands,releases,tracks
FROM band
INNER JOIN releases 
ON band.id = releases.id
inner join track
on band.id = track.id

Use this query to show you only one band:

Declare @releases varchar(50)
Set @releases = 'showbiz'
SElect distinct bands from discograpy where releases = @releases

Here any variable can be passed or set in place of showbiz. This is an example

Comments

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.