Get id value from same table in query - mysql

I have this table setup:
orders:
id, order_nr
orders_cart:
id, orders_id,ref_backorder
Some data:
Orders:
id order_nr
1 012345
2 0123456
Orders_cart
id, orders_id, ref_backorder
1 2 1
1 1
the ref_backorder is a reference to another row in the same table.
Expected output:
id, ref_backorder
1 012345
2 null
My query:
SELECT o.id, o.ref_backorder`
FROM orders_cart o
??
LEFT JOIN orders ON o.ref_backorder` = orders.id
??
How can i get the ref_backorder as order_nr?
ref_backorder is an id from a row. In this case row 1, in row 1 we have an orders_id 2. The value of orders_id 2 (0123456) is the value what I want to show in the query.
Can you join the table to itself?

Seems the query could be this
select o.order_nr
from order
inner join orders_cart as c on c.ref_backorder = o.id
where c.orders_id = 2

SELECT A.id, A.orders_id, A.ref_backorder, B.order_nr FROM orders_cart AS A
JOIN orders AS B ON
A.orders_id = B.id

Related

How can I retrieve Similar Orders In Mysql?

i need a query that should first look the oldest order which has status 0 (zero). and retrieves all the similar orders of that kind(matches exact total qty, itemSku and number of distinct items ordered).
***OrdersTable***
ID OrderNumber CustomerId Status created_at
1 123456 1 0 2018-01-01
2 234567 1 0 2018-01-02
3 345678 1 0 2018-01-03
4 456789 1 0 2018-01-04
***PurchasedProductsTable***
OrderId itemSku Qty
1 1000001 1
1 1000002 2
2 1000001 3
3 1000001 1
3 1000002 2
4 1000001 3
In the above table the query should first look at the oldest (created_at ASC) order (i.e with Id 1) having status 0 (in order table). and along with that order it should retrieves all the other orders that matches the same itemSku, qty and total distinct items count (in purchasedProducts table).
here order 1 and 3 matches the same itemSKu (1000001 and 1000002) and qty ( 1 and 2) and both have (2) distinct items count respectively so order 1 and 3 should be retrived at first.and when i marked order 1 and 3 as shipped (i.e chang status to 2).
and if i run query again it should retrive similar oders. now order 2 and 4 as order 2 and 4 are similar orders. (have same itemSkus (1000001, Qty (3) and distinct items count (1)).
please help thanks
You have to go trough your tables two times :)
Something like this :
SELECT DISTINCT O2.ID
FROM OrdersTable O1
INNER JOIN PurchasedProductsTable P1 ON O1.ID = P1.OrderId
INNER JOIN PurchasedProductsTable P2 ON P1.itemSku = P2.itemSku
AND P1.Qty = P2.Qty
INNER JOIN OrdersTable O2 ON O2.ID = P2.OrderId
WHERE O1.ID =
(SELECT ID FROM OrdersTable WHERE Status = 0
ORDER BY created_at ASC LIMIT 1)
AND (SELECT COUNT(*) FROM PurchasedProductsTable WHERE OrderId = O1.ID)
= (SELECT COUNT(*) FROM PurchasedProductsTable WHERE OrderId = O2.ID)
ORDER BY O2.ID ASC;
https://www.db-fiddle.com/f/65t9GgSfqMpzNVgnrJp2TR/2
You can get the earliest order via a limit and ordered by the date.
Then you can left join to get that order and any other order that at least has the same items.
Then once you have those order id's from the sub-query result, you can get the order details.
SELECT o.*
FROM
(
SELECT DISTINCT ord2.ID as OrderId
FROM
(
SELECT ID, CustomerId, Status
FROM OrdersTable
WHERE Status = 0
ORDER BY created_at
LIMIT 1
) AS ord1
JOIN PurchasedProductsTable AS pprod1
ON pprod1.OrderId = ord1.ID
LEFT JOIN OrdersTable ord2
ON ord2.CustomerId = ord1.CustomerId
AND ord2.Status = ord1.Status
LEFT JOIN PurchasedProductsTable pprod2
ON pprod2.OrderId = ord2.ID
AND pprod2.itemSku = pprod1.itemSku
AND pprod2.Qty = pprod1.Qty
GROUP BY ord1.CustomerId, ord1.ID, ord2.ID
HAVING COUNT(pprod1.itemSku) = COUNT(pprod2.itemSku)
) q
JOIN OrdersTable AS o ON o.ID = q.OrderId;
Test on RexTester here

Check if ID from a table exists in another table, and if so, how many times

Let's say I have two tables
Table a
some_ID
1
2
3
4
Table b
some_ID
1
2
1
4
Now what I would like to receive is a table like
id amount
1 | 2
2 | 1
I tried with a following query:
SELECT COUNT(a.some_id) as id
FROM Table_a
INNER JOIN Table_b
ON Table_a.some_id = Table.b.some_id
but that only returned how many id rows there are in both tables.
Any help?
Do the grouping on table_b and then join that result set on table_a
SELECT b.* FROM
(
SELECT id, COUNT(*) AS Cnt
FROM Table_b
GROUP BY id
) b
INNER JOIN Table_a a ON a.id = b.id
SQLFiddle
If you want the zero counts:
SELECT a.some_id AS id, count(b.some_id) as amount
FROM a LEFT JOIN b ON a.some_id = b.some_id
GROUP BY a.some_id
Result:
id | amount
1 | 2
2 | 1
3 | 0
4 | 1
If not:
SELECT a.some_id AS id, count(*) as amount
FROM a INNER JOIN b ON a.some_id = b.some_id
GROUP BY a.some_id
Result:
id | amount
1 | 2
2 | 1
4 | 1
The difference is the join type. Once left outer join. Then inner join. Note that in the first case it is important to count with count(b.some_id). With count(*) the rows with missing b entries would be counted as 1. count(*) counts the rows. count(expression) counts the non-null values.
If I understand correctly, you want a histogram of histograms:
select cnt, count(*) as num_ids
from (select id, count(*) as cnt
from b
group by id
) b
group by cnt;

Return group_concat with multiple rows

I have a orderLine table, where orders are attached to products. So my orderLine table can look like this:
orderID as coupled to orderLineId as a foreign key.
orderID productID
1 2
2 1
2 2
And my order table is like this:
orderLineId userID
1 1
1 1
2 1
But if I do the GROUP_CONCAT query:
SELECT orderLine.orderId,
GROUP_CONCAT(orderLine.productId)
FROM orders
INNER JOIN orderLine ON orders.orderLineId = orderLine.orderId
WHERE orders.orderId = 1
I get the result:
orderId, GROUP_CONCAT(orderLine.productId)
1 1,2,2
But I want to have:
orderId, GROUP_CONCAT(orderLine.productId)
1 1,2
2 2
You need to GROUP BY orderLine.orderId to get one row for each orderId:
SELECT orderLine.orderId,
GROUP_CONCAT(orderLine.productId)
FROM orders
INNER JOIN orderLine ON orders.orderLineId = orderLine.orderId
WHERE orders.orderId = 1
GROUP BY orderLine.orderId

mysql union problems with subtraction

for example: I have 4 tables:
table 1 : item
item_id name quantity
==========================
1 a 10
2 b 15
table 2 : additional
add_id name quantity item_id
===================================
1 c 5 1
table 3 : item_out (this will subtract item quantity)
item_id quantity
==========================
1 10
2 5
table 4 : additional_out (this will subtract additional quantity)
additional_id quantity
==========================
1 5
My question is: what mysql-query should I give in order to get these result?
item_ id quantity additional_id additinal_quantity
=================================================
1 5 1 0
2 15
because I use mysql query code like:
SELECT item_id, SUM(quantity - IFNULL(number,0)), additional_id, SUM(additional_quantity - IFNULL(additional_number,0))
FROM(
SELECT a.item_id, a.quantity, b.additional_id, b.quantity AS additional_quantity, NULL AS number, NULL AS additional_number
FROM table_1 a JOIN table_2 b ON some condition
UNION
SELECT NULL AS item_id , NULL AS quantity,... a.quantity AS number, b.quantity AS additional_number
FROM table_3 a JOIN table_4 b ON condition
)AS T
and it ends with only 1 row because of SUM in the first selection.
Try this:
SELECT item_ id,
(item.quantity - IFNULL(item_out.quantity,0)) AS quantity,
additional_id,
(IFNULL(a.quantity,0) - IFNULL(ao.quantity,0)) AS additional_quantity
FROM item
LEFT JOIN item_out
ON item.item_id = item_out.item_id
LEFT JOIN additional a
ON item.item_id = a.item_id
LEFT JOIN additional_out ao
ON a.addid = ao.additional_id

Select ONLY one row per category

Ok I have siple table which contains data:
id cat_id title (with random values)
1 1 test
2 1 tstt
3 3 tewt
4 2 4324
5 3 rterter
Now, I need to create a query which selects only ONE raw per category (cat_id)
(possibly with lowest ID and ordered by cat_id)
So the result should be:
1 1 test
4 2 4324
3 3 tewt
Use GROUP BY :
SELECT MIN(id), cat_id, title FROM table GROUP BY cat_id
SELECT a.*
FROM tableName a
INNER JOIN
(
SELECT cat_id, MIN(id) id
FROM tableName
GROUP BY cat_id
) b ON a.cat_id = b.cat_id AND
a.id = b.id
ORDER BY a.cat_id