Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.