Order By in 2 MYSQL tables

Asked

Viewed 250 times

0

Good morning! I am going through more moments and I would like your help with this problem!

Come on, I’m doing a select in MYSQL where several tables are selected, in opencart to generate an XML for facebook ads.

My XML code looks like this

SELECT DISTINCT t1.product_id as product_idT1, t1.manufacturer_id as manufaturer, t1.price as price, t1.date_added as dataAdd, t1.quantity as quantity, t2.name as nameT2, t2.description as descriptionT2, t3.name as nameT3, t4.name as nameT4, t5.image, t6.name as nameT6, t7.query as urlT7, t7.keyword as keyword 
FROM oc_product t1 
INNER JOIN oc_product_description t2 ON (t1.product_id = t2.product_id) 
INNER JOIN oc_manufacturer t3 ON (t1.manufacturer_id = t3.manufacturer_id) 
INNER JOIN oc_option_value_description t4 
INNER JOIN oc_product_image t5 ON (t1.product_id = t5.product_id) 
INNER JOIN oc_stock_status t6 ON (t1.stock_status_id = t6.stock_status_id) 
INNER JOIN oc_url_alias t7 
WHERE t7.query like CONCAT('%', t1.product_id , '%') 
GROUP BY product_idT1 ASC

Only in this case, the picture table oc_product_image needs to be in DECREASING order because I want the last image.

I am using PHP.

Thank you!

  • 1

    ORDER BY T5.image DESC, t1.product_id

  • 1

    Show... thank you so much! But still not what I need =/

  • 2

    Explain better your need, if the solution is not a simple order by, what is your difficulty?

  • I need it to return in descending order Ex: Product 1(oc_product) - Photo 1 (oc_product_image) Product 2(oc_product) - Photo 2 (oc_product_image) But I don’t know why my first photo in the register, it’s getting like the last one! Gave to understand ^^

  • It’s a little confusing yet, but from what I understand it would be simple: ORDER BY t5.product_id DESC. But in your last comment, I honestly had the impression that you were wanting Ascendant order and not Descendant... but see if the solution I posted meets your need.

  • Thank you, that’s right! Thank you very much

  • Reactivating the question again, this query would be to list all products but not this listing, it is hiding about 90 results, could tell me where I am missing?

Show 2 more comments

2 answers

0

I did it this way:

SELECT DISTINCT t1.product_id as product_idT1, t1.manufacturer_id as manufaturer, t1.price as price, t1.date_added as dataAdd, t1.quantity as quantity, t2.name as nameT2, t2.description as descriptionT2, t3.name as nameT3, t4.name as nameT4, t5.image as imagemT5, t6.name as nameT6, t7.query as urlT7, t7.keyword as keyword, t8.product_id, t8.price as produtoDesconto 
FROM oc_product t1 
LEFT JOIN oc_product_description t2 ON (t1.product_id = t2.product_id) 
LEFT JOIN oc_manufacturer t3 ON (t1.manufacturer_id = t3.manufacturer_id) 
LEFT JOIN oc_option_value_description t4 
LEFT JOIN oc_product_image t5 ON (t1.product_id = t5.product_id) 
LEFT JOIN oc_stock_status t6 ON (t1.stock_status_id = t6.stock_status_id) 
LEFT JOIN oc_product_special t8 ON (t1.product_id = t8.product_id)
LEFT JOIN oc_url_alias t7 
ORDER BY t5.product_id DESC

Return me the following error: #1064 - You have an error in your SQL syntax; check the manual that Corresponds to your Mysql server version for the right syntax to use near 'ORDER BY T5.product_id DESC LIMIT 0, 25' at line 10

Can you help me with what I’m doing wrong? Thank you

  • You should not post a question about a problem in response. Either edit your initial question or use the comments. But in this SQL note that the JOIN with the table oc_url_alias T7 does not have the clause ON (It is necessary!)

0


Do ORDER BY t5.product_id DESC, so order by the order of registration of the product.
The fact that you are not listing all products, must be related to INNER JOINS or even to WHERE.
Exchange the Inners for LEFT JOIN (test only) and remove the Where.
So you will discover what is preventing you from fetching all products.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.