Notices
Computer & Technology Related Post here for help and discussion of computing and related technology. Internet, TVs, phones, consoles, computers, tablets and any other gadgets.

SQL and joins/group by

Thread Tools
 
Search this Thread
 
Old 25 June 2005, 02:42 PM
  #1  
RichB
Scooby Regular
Thread Starter
 
RichB's Avatar
 
Join Date: Apr 1999
Location: Bore Knee Muff
Posts: 3,666
Likes: 0
Received 0 Likes on 0 Posts
Default SQL and joins/group by

Quick question, its driving me mad...

Two tables.

Table1 - items

item_id
item_name
item_enabled

Table2 - pics
pic_id
pic_item_id
pic_main
pic_filename


Example data;
table 1
1,car,1
2,boat,0
3,car2,0
4,car3,1

table2
1,1,0,pic1
2,1,0,pic2
3,1,1,pic3
4,1,0,pic4
5,4,1,pic5

I want to show only the main image in a list.
Result required.
1,car,pic3
4,car3,pic5

Result I am getting so far
1,car,pic1
4,car3,pic5


Im using mysql.I want the out put to show all items whether they have an image or not in a list, against each item I want to show the image that it set to be the main image.

I want something like:
SELECT * FROM items
LEFT JOIN pics
ON items.item_id=pics.pic_item_id
WHERE items.item_enabled=1
GROUP BY pics.pic_main

This partly works as it give me the list of all items but I can't get it to only give me the ones where pic_main is set...

Can anyone shed any light on this?

Cheers,
Rich
Old 25 June 2005, 04:23 PM
  #2  
RichB
Scooby Regular
Thread Starter
 
RichB's Avatar
 
Join Date: Apr 1999
Location: Bore Knee Muff
Posts: 3,666
Likes: 0
Received 0 Likes on 0 Posts
Default

got it now but spent all damn day on this...
Old 25 June 2005, 07:51 PM
  #3  
Jim_B
Scooby Regular
iTrader: (1)
 
Jim_B's Avatar
 
Join Date: Oct 2001
Location: Bristol
Posts: 508
Likes: 0
Received 0 Likes on 0 Posts
Default

Wouldnt this have done what you want - if you only want results with pic_main set ?
select * from pics,items
where items.item_id = pics.pic_item_id
and pics.pic_main = 1
and items.item_enabled = 1
Old 25 June 2005, 08:20 PM
  #4  
RichB
Scooby Regular
Thread Starter
 
RichB's Avatar
 
Join Date: Apr 1999
Location: Bore Knee Muff
Posts: 3,666
Likes: 0
Received 0 Likes on 0 Posts
Default

no because "I want the out put to show all items whether they have an image or not in a list" ... and to use the one marked as pic_main if it is set...
Sorry not too clear on my description...

I've now completely broken my installation of mySql somehow...... not a great day - LOL
Old 25 June 2005, 09:07 PM
  #5  
GaryK
Scooby Regular
 
GaryK's Avatar
 
Join Date: Sep 1999
Location: Bedfordshire
Posts: 4,037
Likes: 0
Received 0 Likes on 0 Posts
Default

Yep an INNER join like you have in your SQL will only return rows where there is a match between the two tables and an OUTER join will return rows if there is a match or not so just add the OUTER keyword.

Gary
Old 26 June 2005, 01:06 AM
  #6  
RichB
Scooby Regular
Thread Starter
 
RichB's Avatar
 
Join Date: Apr 1999
Location: Bore Knee Muff
Posts: 3,666
Likes: 0
Received 0 Likes on 0 Posts
Default

Actually Gary, with mysql that is an outer join.

I get all the items whether they have a main image or not, its just getting the item with the name of the main image if there was more than one.

I have done it now by getting all the main images first into a temporary table then getting all the items and doing a left join with the temp table.
the problem was i had got the SQL working using the phpmyadmin front end but that code then didnt work on my page. Weird but I've sorted it now.
Old 26 June 2005, 10:16 PM
  #7  
Dream Weaver
Scooby Regular
 
Dream Weaver's Avatar
 
Join Date: Feb 2000
Location: Lancashire
Posts: 9,844
Received 0 Likes on 0 Posts
Default

Why not just loop through the items recordset and pull the images in from the while loop?

e.g

while not RSitems.EOF

'Choose pic here limiting to 1 picture

Show RSitems info here

RSitems.movenext
wend

etc etc
Related Topics
Thread
Thread Starter
Forum
Replies
Last Post
gregh
Computer & Technology Related
5
02 April 2003 01:44 PM
MrDeference
Computer & Technology Related
3
30 January 2003 10:56 PM
Jeff Wiltshire
Computer & Technology Related
2
25 January 2003 09:53 PM
Andy Tang
Computer & Technology Related
2
22 January 2003 09:54 PM
Puff The Magic Wagon!
Computer & Technology Related
3
02 February 2002 04:04 PM



Quick Reply: SQL and joins/group by



All times are GMT +1. The time now is 02:08 AM.