Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

hiandy's avatar

Looking for assistence with simple change of SQL query

You need to know, these two tables are connected by ORDER_ID:

//TABLE order_products
id |	order_id

//TABLE order_deliverydates
id|	order_id	| calculated


I want sort this query by CALCULATED:

SELECT
            	op.id, ........... os.shipping,
                swe.note AS custom_note
			FROM order_products op
            LEFT JOIN order_shipping os ON os.batch_id = op.batch_id AND os.order_id = op.order_id
            LEFT JOIN some_wee_empty swe ON TRIM(swe.name) LIKE TRIM(op.name)
			WHERE op.order_id NOT IN (
				SELECT op.order_id
				FROM order_products op
				WHERE op.completed < op.quantity OR op.sent = 1
			)
            ' . $this->filter($customerFilter) . '
			GROUP BY op.order_id, op.id
			ORDER BY op.date ASC, op.id
0 likes
5 replies
Tray2's avatar

Your table and query are not showing the same thing here so it makes no sense to me.

However joining the two tables together and order by one of the values is simple.

SELECT op.order_id, od.calculated
FROM order_products op,
	    order_deliverydates od
WHERE op.order_id = od.order_id
ORDER BY od.calculated
hiandy's avatar

TABLE order_deliverydates is not used in original query at all.

I understand what you wrote in your example, but I'm not sure how to implement it into that query.

Tray2's avatar

Show us all your tables, some data and the expected result.

hiandy's avatar

It's a working query - I don't understand what doesn't make sense about it?

I just need to solve that sorting.

Tray2's avatar

Since I don't know what the data looks like and what you expect to get back it's very hard to help you.

Please or to participate in this conversation.