Jump to content

LEFT JOIN problem


sniperscope

Recommended Posts

Hi

I have join problem again.  :'(

 

Lets say i have a TABLE_A and TABLE_B

I want to get all data from TABLE_A but sort by TABLE_B

I have and sql query but it returns all data from TABLE_A but repeat it number of TABLE_B

I know there is a simple but i cannot see it (i didn't sleep more than 50 hours maybe because of that)

My query is :

SELECT * FROM shop s LEFT JOIN search_up ss ON s.id = ss.shop_id WHERE s.shop_region = 'kansai' AND s.publication = 'dsp' AND (s.shop_area = 'osaka') ORDER BY ss.add_date DESC LIMIT 0,5 

 

Thanks for any help.

Link to comment
Share on other sites

the issue is

query grab data from TABLE_A but repeat same data number of TABLE_B

 

For example:

I have 5 records in TABLE_B and i want to get phpfreaks.com, amazon.com, google.com, yahoo.com data from TABLE_A

This query brings me phpfreaks.com, amazon.com, google.com, yahoo.com 5 times

 

All i want is get data from TABLE_A and find out which data has The Latest entry.

Link to comment
Share on other sites

SELECT * FROM shop s
LEFT JOIN ( SELECT ss.shop_id, MAX(add_date) AS add_date FROM search_up ss GROUP BY shop_id ) ss ON s.id = ss.shop_id
WHERE s.shop_region = 'kansai' AND s.publication = 'dsp' AND (s.shop_area = 'osaka')
ORDER BY ss.add_date DESC LIMIT 0,5

 

Please Note: You should never use SELECT * even if there are a large number of fields.

 

~awjudd

Link to comment
Share on other sites

SELECT * FROM shop s
LEFT JOIN ( SELECT ss.shop_id, MAX(add_date) AS add_date FROM search_up ss GROUP BY shop_id ) ss ON s.id = ss.shop_id
WHERE s.shop_region = 'kansai' AND s.publication = 'dsp' AND (s.shop_area = 'osaka')
ORDER BY ss.add_date DESC LIMIT 0,5

 

Please Note: You should never use SELECT * even if there are a large number of fields.

 

~awjudd

 

Marvelous.

It is working now. Thank you so much for help.

 

Have a great day

Link to comment
Share on other sites

SELECT * FROM shop s
LEFT JOIN ( SELECT ss.shop_id, MAX(add_date) AS add_date FROM search_up ss GROUP BY shop_id ) ss ON s.id = ss.shop_id
WHERE s.shop_region = 'kansai' AND s.publication = 'dsp' AND (s.shop_area = 'osaka')
ORDER BY ss.add_date DESC LIMIT 0,5

 

Please Note: You should never use SELECT * even if there are a large number of fields.

 

~awjudd

 

Dear awjudd

Thanks for your code. It was worked.

But, i would like to ask that what if i want to sort it by two columns.

 

I changed your query as below but it fails.

Here is what i changed.

 

PS: I took your advice and used column name in query

 

"SELECT 
			s.id, 
			s.copy, 
			s.name, 
			s.type, 
			s.address_short, 
			s.access, 
			s.salary, 
			s.qualification, 
			s.staff_only, 
			s.opstions, 
			s.picture, 
			s.main_txt, , 
			ss.add_time, 
			ss.add_date 
		FROM shops s 
		LEFT JOIN ( SELECT ss.shop_id, MAX(add_time), AS add_time MAX(add_date) AS add_date FROM search_up ss GROUP BY shop_id ) ss 
		ON s.id = ss.shop_id
		WHERE s.shop_region = 'kansai' AND s.publication = 'dsp' AND (s.shop_area = 'osaka')"

Link to comment
Share on other sites

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.