Jump to content

[solved] Need help with a SQL query


NSN

Recommended Posts

I'm trying to make a SQL query, but I can't figure out how it works. (I have learned to handle HTML, CSS, PHP, etc., but have never understood SQL)

What I want to achieve is a list with the following content:

name of supplier   -   product name   -   product reference   -   product qty 

There should be an option to enter the order id so that I get the list for a couple of orders combined, grouped by article and sorted by supplier.
My foolish try ended as follows, of course with nor result.

SELECT
s.name AS supplier,
od.product_name AS product,
od.product_reference AS sku,
od.product_quantity AS qty,
FROM `ps_order_detail` od
LEFT JOIN `ps_order_detail` od ON o.`id_order` = od.`id_order`
LEFT JOIN `ps_supplier` od ON s.`product_reference` = od.`product_reference``
GROUP BY od.`product_name`
SORT BY s.name ASC
WHERE od.`id_order` =1166,1167,

Edited by NSN (see edit history)
Link to comment
Share on other sites

I don't have a database available to check. But it is obvious that the following doesn't make sense:

FROM `ps_order_detail` od
LEFT JOIN `ps_order_detail` od ON o.`id_order` = od.`id_order`

There is no need to insert ps_order_detail twice and you never include a ps_order table or define an "o" shortcut.

Also you can't write

WHERE od.`id_order` =1166,1167, 

it should be "where id_order=1166 or id_order=1167". Alternatively you could use an array.

Edited by musicmaster (see edit history)
Link to comment
Share on other sites

Am 11.12.2018 um 7:49 AM schrieb musicmaster:

I don't have a database available to check. But it is obvious that the following doesn't make sense:


FROM `ps_order_detail` od
LEFT JOIN `ps_order_detail` od ON o.`id_order` = od.`id_order`

There is no need to insert ps_order_detail twice and you never include a ps_order table or define an "o" shortcut.

Also you can't write


WHERE od.`id_order` =1166,1167, 

it should be "where id_order=1166 or id_order=1167". Alternatively you could use an array.

Sorry for my late reply. I totally missed your answer.
Thanks for your input. I was able to create the query with some help from a forum member and have afterwards modified the query a bit.

It looks no like this

SELECT s.name AS Supplier, od.product_reference AS SKU, m.name AS Manufacturer, od.product_name AS Name, SUM(od.product_quantity) AS Quantity
    FROM ps_order_detail od
    LEFT JOIN ps_product p ON (p.id_product = od.product_id)
    LEFT JOIN ps_supplier s ON (s.id_supplier = p.id_supplier)
    LEFT JOIN ps_manufacturer m ON (m.id_manufacturer = p.id_manufacturer)
    LEFT JOIN ps_category c ON (c.id_category = p.id_category_default)
WHERE od.id_order=1166 OR od.id_order=1167
GROUP by od.product_reference
ORDER BY  s.name ASC, c.id_category, od.product_name ASC

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...