Jump to content

Assistance with MySQL JOIN statement


whit3fir3

Recommended Posts

I am working on a project and I am having problems with a JOIN statement.  The following statement pulls information from a total of 5 tables.  The first 4 joins work exactly like I was them to.  It is the join with the Img table that I giving me problems.  The Img table is designed to store images related to the corresponding record in the table Cards.  I wanted it to be a One to Many relationship so that each card record could have multiple images.  The problem I am having is when a Card record has multiple images images that Card record is being repeated.  This is not what I want it to do.  I just want it to show up once.  Does anyone know what I am doing wrong with my JOIN statement?

 

SELECT t1.id as 'id',t3.name as 'Brand',t2.Name as 'Sport',t1.fname,t1.lname,
t1.cardnum,t1.year1,t1.year2,t1.price,t1.qty,t4.cond as 'Cond',t1.beckett_price,
t1.price_paid as 'paid', t5.landscape as landscape FROM `Cards`
as t1 INNER JOIN `Sports` as t2 on t2.id=t1.sportID INNER JOIN `Brand`
as t3 on t3.id=t1.brandID INNER JOIN `Con` as t4 on t4.id=t1.cond LEFT OUTER JOIN `Img` as 
t5 on t5.cardID=t1.id ORDER BY id DESC LIMIT 100

 

Thanks,

 

Whit3fir3

Link to comment
https://forums.phpfreaks.com/topic/184361-assistance-with-mysql-join-statement/
Share on other sites

Apply GROUP BY on t1.id

SELECT t1.id as 'id',t3.name as 'Brand',t2.Name as 'Sport',t1.fname,t1.lname,
t1.cardnum,t1.year1,t1.year2,t1.price,t1.qty,t4.cond as 'Cond',t1.beckett_price,
t1.price_paid as 'paid', t5.landscape as landscape FROM `Cards`
as t1 INNER JOIN `Sports` as t2 on t2.id=t1.sportID INNER JOIN `Brand`
as t3 on t3.id=t1.brandID INNER JOIN `Con` as t4 on t4.id=t1.cond LEFT OUTER JOIN `Img` as 
t5 on t5.cardID=t1.id 
GROUP BY t1.id
ORDER BY id DESC LIMIT 100

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.