Mysql join three tables (2 of them similar) - mysql

my customer table
id | name
==========
1 | mick
2 | george
3 | john
bakery transactions
id | customer | type | amount | date
============================================
1 | 3 | bread | 1 | 2016/03/10 10:00
2 | 1 | bread | 2 | 2016/03/10 11:00
3 | 1 | baguette| 1 | 2016/03/10 11:00
4 | 2 | bread | 2 | 2016/03/11 09:00
5 | 3 | cookie | 5 | 2016/03/11 09:30
greengrocery transactions
id | customer | type | amount | date
============================================
1 | 1 | banana | 1 | 2016/03/10 08:00
2 | 1 | apple | 3 | 2016/03/10 08:00
3 | 1 | orange | 5 | 2016/03/10 14:00
4 | 3 | apple | 8 | 2016/03/11 09:30
5 | 1 | apple | 8 | 2016/03/12 09:30
Is it possible to get customer transactions by date from these transaction tables by the customer?
More specific, I want to get customer Id:1 transactions, ordered by date;
This is what I want.
Id| transactionType|customer | type | amount | date
=====================================================================
1 | greengrocery | 1 | banana | 1 | 2016/03/10 08:00
2 | greengrocery | 1 | apple | 3 | 2016/03/10 08:00
2 | bakery | 1 | bread | 2 | 2016/03/10 11:00
3 | bakery | 1 | baguette| 2 | 2016/03/10 11:00
4 | greengrocery | 1 | orange | 5 | 2016/03/10 14:00
5 | greengrocery | 1 | apple | 8 | 2016/03/12 09:30

You basically need two joined queries unioned together, like this:
SELECT * FROM (
SELECT t.id,'bakery' as TransType,s.id as customer,t.type,t.amount,t.date
FROM customer s
INNER JOIN bakery t ON(s.id = t.customer)
UNION ALL
SELECT t.id,'greengrocery' as TransType,s.id as customer,t.type,t.amount,t.date
FROM customer s
INNER JOIN greengrocery t ON(s.id = t.customer)) tt
WHERE tt.customer = 1
order by tt.date
Basically, you don't even need to select from customer because your are not using the name value, so it can be done like this:
SELECT * FROM (
SELECT t.id,'bakery' as TransType,t.customer as customer,t.type,t.amount,t.date
FROM bakery t
UNION ALL
SELECT t.id,'greengrocery' as TransType,t.customer as customer,t.type,t.amount,t.date
FROM greengrocery t) tt
WHERE tt.customer = 1
order by tt.date

You'd be better off having one table for transactions with a category field
id | category_id | customer | type | amount | date
============================================
1 | 1 | 3 | bread | 1 | 2016/03/10 10:00
And then a category table like:
id | category_name |
====================
1 | bakery
You can then do a join like so:
SELECT transactions.id, category_name AS transactionType, customer.name, transactions.type, tranasactions.amount, transactions.date
FROM transactions
LEFT JOIN categories ON transactions.category_id=categories.id
LEFT JOIN customers ON transactions.customer=customer.id

Use UNION ALL to generate the rows and use a variable to generate the new id.
SET #id = 0;
( SELECT #id:=#id+1 id, 'bakery', customer, type, amount, date
FROM bakery_transactions
WHERE customer = 1 )
UNION ALL
( SELECT #id:=#id+1 id, 'greengrocery', customer, type, amount, date
FROM greengrocery_transactions
WHERE customer = 1 )
ORDER BY date

Related

Count nested select using join and by month

Hi I have three tables here is all
First. count_internets
id| company_id | item_id | created_at
1 | 1 | 1 | 2020-10-14 |
2 | 1 | 2 | 2020-10-15 |
3 | 2 | 4 | 2020-10-16 |
4 | 2 | 5 | 2020-11-20 |
5 | 1 | 1 | 2020-11-22 |
6 | 1 | 1 | 2020-11-23 |
7 | 2 | 5 | 2020-11-23 |
Second compaies
id | name
1 | Company 1
2 | Company 2
Third items
id | name | company_id
1 | Product 1 | 1
2 | Product 2 | 1
3 | Product 4 | 2
4 | Product 5 | 5
I want to get infomartion how many items sold during one month grouping by company like below
| Company | Products | Month | Count
| Company 1 | Product 1 | OCT | 1
| Company 1 | Product 2 | OCT | 1
| Company 1 | Product 1 | NOV | 1
| Company 1 | Product 4 | OCT | 1
| Company 2 | Product 5 | NOV | 2
I tried many SQL queries, but I can not solve. Please help to solve this query.
This one should work
SELECT c.name as company, i.name as products, EXTRACT(MONTH FROM FROM_UNIXTIME(created_at)) as month, count(*)
FROM count_internets
JOIN companies as c on company_id = c.id
JOIN items as i on item_id = i.id
GROUP BY company, products, month
Be aware that if that the query extract only the month (so no difference between years). If you need the year too you have to extract it EXTRACT(YEAR FROM FROM_UNIXTIME(created_at)) as year.
But your example is not very clear, company 1 didn't sell product 3. Furthermore, what's the meaning of the company_id in items table?

SQL Query COUNT SELECT

I need to generate table as stated below. I have these samples but it doesn't work. Any please help.
SELECT id, room, bed,
bed -(SELECT count(bed) FROM tb_student WHERE room_id = id) as FREE
FROM tb_rooms
tb_rooms
+----+-------+-----+
| ID | ROOM | BED |
+----+-------+-----+
| 1 | A111 | 4 |
| 2 | A112 | 2 |
| 3 | A113 | 2 |
| 4 | A114 | 2 |
+----+-------+-----+
tb_student
+----+---------+----------+
| ID | STUD_ID | ROOM_ID |
+----+---------+----------+
| 1 | 211 | 3 |
| 2 | 212 | 1 |
| 3 | 213 | 1 |
| 4 | 214 | 2 |
+----+----------+---------+
I need something like this...
+----+-------+------+-----+
| ID | ROOM | BED |FREE |
+----+-------+------+-----+
| 1 | A111 | 4 | 2 |
| 2 | A112 | 2 | 1 |
| 3 | A113 | 2 | 1 |
| 4 | A114 | 2 | 2 |
+----+-------+------+-----+
try this
SELECT id as id, room as room, bed as bed,
bed -( SELECT count(Room_ID) FROM tb_student where room_id = soh.id) as FREE
FROM tb_rooms soh
good luck
hello john your query right only create alias of your table
see here your query :
SELECT id as id, room as room, bed as bed,
bed -( SELECT count(*) FROM tb_student where room_id = tr.id) as FREE
FROM tb_rooms tr
You can try this:
SELECT
r.ID,
r.ROOM,
r.BED,
r.BED - COUNT(DISTINCT s.ID) AS FREE
FROM tb_rooms AS r
LEFT JOIN tb_student AS s ON r.ID = s.ROOM_ID
GROUP BY r.ID, r.ROOM, r.BED
Here is the SQLFIDDLE.

Combine multiple select statement in one result table

I have two tables, one for sales and another for stock.
I want to select location id, item id, size id and sales qty from sales table, while I want just to select stock qty from stock table for the same location id and size id from sales table, like this:
Sales table:
------------------------------------
| loc_id | item_id | size_id | qty |
------------------------------------
| 5 | 11321 | 1 | 5 |
| 5 | 11321 | 2 | 8 |
| 5 | 11321 | 3 | 4 |
| 5 | 11321 | 2 | 1 |
Stock table:
------------------------------------
| loc_id | item_id | size_id | qty |
------------------------------------
| 5 | 11321 | 1 | 3 |
| 5 | 11321 | 2 | 7 |
| 5 | 11321 | 3 | 9 |
So the result after select should be like this:
------------------------------------------------------
| loc_id | item_id | size_id | sales_qty | stock_qty |
------------------------------------------------------
| 5 | 11321 | 1 | 5 | 3 |
| 5 | 11321 | 2 | 9 | 7 |
| 5 | 11321 | 3 | 4 | 9 |
Here's what I tried to do:
SELECT SUM(T1.qty) AS `salesQty`, SUM(T2.qty) AS `stockQty`, T1.size_id,
T1.loc_id
FROM sales T1
INNER JOIN stock T2 ON T2.item_id = T1.item_id AND T2.size_id = T1.size_id
WHERE T1.item_id = '11321'
AND T1.size_id IN (1,2,3)
AND T1.loc_id IN (5)
GROUP BY T1.size_id, T1.loc_id
But stock qty always wrong!
select
q1.loc_id
,q1.item_id
,q1.size_id
,sum(case when q1.Type='Sales' then q1.Qty else 0 end) as sales_qty
,sum(case when q1.Type='Stock' then q1.Qty else 0 end) as stock_qty
from (
select
T1.loc_id
,T1.item_id
,T1.size_id
,'Sales' as Type
,SUM(T1.qty) AS Qty
from sales T1
group by
T1.loc_id
,T1.item_id
,T1.size_id
union all
select
T2.loc_id
,T2.item_id
,T2.size_id
,'Stock' as Type
,SUM(T2.qty) AS Qty
from stock T2
group by
T2.loc_id
,T2.item_id
,T2.size_id) q1
group by
q1.loc_id
,q1.item_id
,q1.size_id

SQL indexing on lines from joined table on each record from the original

I have two tables.
Orders (o)
id | buyer
1 | Joe
2 | Ann
3 | Sue
And Order_Items (oi)
id | master_record_id | stock number | qty
1 | 1 | 1234 | 1
2 | 1 | 7890 | 1
3 | 1 | 4987 | 1
4 | 2 | 1234 | 1
5 | 2 | 7890 | 1
6 | 2 | 4987 | 1
7 | 3 | 1234 | 1
8 | 3 | 7890 | 1
9 | 3 | 4987 | 1
Table oi is dependable on orders by oi.master_record_id. Every record (id) in table oi is a unique one (there is no storing of which item this is on an order) so for the example I have 3 orders each with 3 lines and in table oi I have 9 records with ids 1 to 9.
My question is what query should I use to be able to export the orders (a line for each ordered item) where each oi has an index based on the order it is connected to:
Example output:
o.id | o.buyer | Required: Item on Order | oi.stock_number
1 | Joe | 1 | 1234
1 | Joe | 2 | 7890
1 | Joe | 3 | 4987
2 | Ann | 1 | 1234
2 | Ann | 2 | 7890
2 | Ann | 3 | 4987
3 | Sue | 1 | 1234
3 | Sue | 2 | 7890
3 | Sue | 3 | 4987
Many Thanks,
Dani
SELECT id,buyer,Item_on_Order,stock_number
FROM
(SELECT O.`id` as id,
O.`buyer` as buyer,
(#row_num := IF(#category=O.`id`,#row_num+1,1)) as Item_on_Order,
OI.`stock_number` as stock_number,
#category := O.`id` AS Temp
FROM
Orders O
INNER JOIN Order_Items OI ON O.`id` = OI.`master_record_id`)T
Hope this helps.
In MySQL, you can use "user variables" to generate row numbers. Generate row numbers for your oi records and join with your Orders table to get your results. Here is a good blog post on how to achieve row numbering in MySQL.

MySQL select a row from a daterange excluding the year

I'm trying to create a MySQL query to select the daily price from a table that is between a date range from another. I only want to use 'starting-ending' months and days from the table "seasons" and I want to pass the year dynamically to the query.
This is my query: (I'm giving it the Year to exclude the one on the table)
SELECT a.season, b.base_price
FROM seasons a
JOIN pricebyseason b ON a.id=b.season_id
WHERE b.prop_id='6' AND '2015-11-29' BETWEEN DATE_FORMAT(a.starting,'2015-%m-%d') AND DATE_FORMAT(a.ending,'2016-%m-%d')
ORDER BY b.base_price DESC
It works but not with all dates.
These are the tables:
seasons (these are static date values)
+----+--------------+------------+------------+
| id | season | starting | ending |
+----+--------------+------------+------------+
| 1 | Peak Season | 2015-12-11 | 2016-01-09 |
| 2 | High Season | 2015-11-27 | 2016-04-15 |
| 3 | Mid Season | 2015-04-16 | 2015-09-01 |
| 4 | Low Season | 2015-09-02 | 2015-11-26 |
| 5 | Spring Break | 2015-03-05 | 2015-03-21 |
+----+--------------+------------+------------+
pricebyseason
+----+---------+-----------+------------+
| id | prop_id | season_id | base_price |
+----+---------+-----------+------------+
| 1 | 6 | 1 | 950 |
| 2 | 6 | 2 | 750 |
| 3 | 6 | 3 | 450 |
| 4 | 6 | 4 | 400 |
| 5 | 6 | 5 | 760 |
+----+---------+-----------+------------+
What I want to achive is query the dialy price between checkin, checkout selection
I create this sqlfiddle: http://sqlfiddle.com/#!9/4a6f4
This is a previuos query that is not working either:
SELECT a.base_price,b.season,b.starting,b.ending
FROM pricebyseason a JOIN seasons b ON a.season_id=b.id
WHERE a.prop_id='6' AND
(DATE_FORMAT(b.starting,'%m-%d') <= '12-27' OR DATE_FORMAT(b.starting,'2016-%m-%d') >= '2015-12-27')
AND
(DATE_FORMAT(b.ending,'%m-%d') >= '12-27' OR DATE_FORMAT(b.ending,'2016-%m-%d') <= '2015-12-27')
ORDER BY base_price DESC
And here are some sample dates for each season: '2016-01-08','2015-12-27','2016-04-14','2015-11-29','2016-04-15','2015-09-01','2016-09-02','2015-11-26','2016-10-10','2016-03-18','2016-06-22','2015-06-15'
Thank a lot