I have three tables.
Table 1
Transact Item Status
0 A 1
1 A 1
2 B 1
3 C 0
...
Table 2
Item Item Name
A shoe1
B shoe2
C shoe3
...
Table 3
Transact Price
0 23
1 22
2 10
3 50
How do I get the transaction history for ALL the items? It is easy to get it for one particular item by entering the item or its name, but how about for ALL the items? Presumably, I wish to plot the transaction history for every item, for ALL the items?
Thanks!
SELECT t1.* , t2.ItemName , t3.Price
FROM Table1 t1
INNER JOIN Table2 t2 ON t1.Item = t2.Item
INNER JOIN Table3 t3 ON t1.Transact = t3.Transact
Related
I'm trying to learn SQL, and while I'm slowly learning how to query data, I'm stuck on querying the results of a query. Take this example
I want an SQL statement to do 2 things. Suppose I have 2 tables like the one below (table 1 borrowed from another example on stack overflow)
Table 1:
ID game point time
1 x 5 7:00
1 z 4 11:00
2 y 6 9:00
3 x 2 2:00
3 y 5 4:00
3 z 8 6:00
4 k 0 8:00
Table 2:
id tv chan
1 cab
2 trop
3 start
4 cab
The first thing I want to do is combine certain columns from these tables. I know I can select these columns and do an inner join on ID
However the second thing I want to do is drop all the rows with point value 0, and then have only rows with distinct game name with the lowest point value. So I want the final table to look like this
id game point tv chan
1 z 4 cab
2 y 5 trop
3 x 2 start
Thanks
You could try something like this:
SELECT t1.ID,
t1.game,
t1.point,
t2.tv_chan
FROM Table1 AS t1
INNER JOIN Table2 AS t2 ON t2.id = t1.id
INNER JOIN (SELECT t11.game, MIN(t11.point) AS min_point
FROM Table1 AS t11
WHERE t11.point != 0
GROUP BY t11.game
) AS t3 ON t3.game = t1.game
AND t3.min_point = t1.point
WHERE t1.point != 0
You could use a join with a subquery that group by id and game for obtain the min point
select t1.id, t1.game. t1.point, t2 `tv chan`
from (
select id, game, min(point) point
from table1
where point > 0
group by id, game
) t1
inner join table2 t2 on t1.id = t2.id
I have results:
item_id subitem_id
----------------------
1 35
1 25
1 8
2 10
2 25
3 60
4 35
4 25
4 44
5 1
5 23
5 15
5 13
5 9
and I have two lists of subitem
(25,44,1)
(8,9)
how do I set the where clause in order to filter the result and return this
item_id subitem_id
----------------------
1 35
1 25 <-- first set
1 8 <-- second set
-----------------
5 1 <-- first set
5 23
5 15
5 13
5 9 <-- second set
because this item_id contain both subitem_id from two lists
SELECT
`item_id`
FROM table
WHERE `subitem_id` in (25,44,1)
AND `subitem_id` in (8,9)
Did not work, because in one time subitem_id have one id (not all list)
P.S.
This is a simple example, in reality we have more than 100k records with some join construction
http://sqlfiddle.com/#!9/71c28e5/3
SELECT t1.*
FROM (
SELECT DISTINCT(t1.item_id)
FROM t1
INNER JOIN t1 t2
ON t1.item_id = t2.item_id
AND t2.subitem_id in (8,9)
WHERE t1.subitem_id in (25,44,1)
) t
LEFT JOIN t1
ON t.item_id = t1.item_id
Another approach to avoid big number of executed records for mysql:
http://sqlfiddle.com/#!9/71c28e5/10
SELECT t1.*
FROM t1
WHERE item_id in (
SELECT DISTINCT(t1.item_id)
FROM t1
INNER JOIN t1 t2
ON t1.item_id = t2.item_id
AND t2.subitem_id in (25,44,1)
WHERE t1.subitem_id in (8,9)
)
SQL Fiddle
I think you're trying to make sure a item_ID has subcategories in 2 differen sets..
Select * from table A
where exists (Select 1 from table B where A.Item_Id = B.Item_ID and subitem_ID in (25,44,1))
and exists (Select 1 from table C where A.Item_Id = C.Item_ID and subitem_ID in (8,9))
I've two tables named table1 and table2. table2 may have elements from table1. I'd like to show all the results from table2 with the price if available in table1 and with status 1. If no product matches in table1 and also status is 0, need to return price as 0.
table1
id pname item_code price status
1 product1 abcd 200 1
2 product2 pqrs 500 1
3 product3 wxyz 425 1
4 product5 mnop 100 0
and table2 as follows
id item_code
10 efgh
11 abcd
12 pqrs
13 mnop
I have tried following query
SELECT `t2`.`id`, `t2`.`item_code`, `t1`.`price`,`t1`.`pname`, COUNT(t2.item_code) AS sellers FROM (`table2` as t2) LEFT JOIN `table1` as t1 ON `t1`.`item_code` = `t2`.`item_code` WHERE `t1`.`status` = 1 GROUP BY `t2`.`item_code`
but it returns common values in table1 and table2 with status 1, but I need all records from table2 with price as 0 if nothing match in table1 or status 0 in table1.
Expected output
id item_code price
10 efgh 0
11 abcd 200
12 pqrs 500
13 mnop 0
Any help please.
Thanks,
Not sure about your current query which is having count and group by however you can do as below
select
t2.id,
t2.item_code,
case
when t1.status = 1 then t1.price
else 0
end as price
from table2 t2
left join table1 t1 on t1.item_code = t2.item_code
Now note that if table1 has multiple matching values from table2 in that case we may need grouping data.
Try below query:
SELECT `t2`.`id`, `t2`.`item_code`, `t1`.`price`,`t1`.`pname`, COUNT(t2.item_code) AS sellers FROM (`table2` as t2) LEFT JOIN (select * from table1 where status = 1)t1 ON `t1`.`item_code` = `t2`.`item_code` GROUP BY `t2`.`item_code`
Move the part of the WHERE clause that checks the left joined table to the ON clause
SELECT `t2`.`id`, `t2`.`item_code`, COALESCE(`t1`.`price`, 0),`t1`.`pname`, COUNT(t2.item_code) AS sellers
FROM (`table2` as t2)
LEFT JOIN `table1` as t1
ON `t1`.`item_code` = `t2`.`item_code`
AND`t1`.`status` = 1
GROUP BY `t2`.`item_code`
I have the following mysql table. I have been trying to select all the rows which are related with the row that has B as value in the code column. The relation is based on the value of the column trans_id
id trans_id code amount side
1 1 A 200 left
2 1 B 200 right
3 2 J 100 right
4 2 C 100 right
5 2 B 200 left
6 3 A 630 right
7 3 K 630 left
My Expected Result:
id trans_id code amount side
1 1 A 200 left
3 2 J 100 right
4 2 C 100 right
Could you please tell me what should be the mysql query to achieve this?
Thanks :)
The following query should return the results you want. This uses a select query to return results to the WHERE clause.
SELECT * FROM yourTable
WHERE trans_id IN (
SELECT trans_id FROM yourTable WHERE code='B'
)
AND code!='B'
Your question is unclear, but as far as I understand, you could use a self join like this:
select a.id,
a.trans_id,
a.code,
a.amount,
a.side
from table as a
inner join table as b on (a.id=b.trans_id and b.code='B');
This will return the row with table.id=2:
id trans_id code amount side
2 1 B 200 right
select
t1.*
from
table_name t1
inner join table_name t2 on t1.trans_id = t2.trans_id
where
t2.code = 'B' and
t2.code <> t1.code
If I'm understanding your problem correctly then a subquery would get you what you need.
SELECT * FROM yourTable WHERE id IN (SELECT trans_id FROM yourTable WHERE code='B')
Let's say i have two tables, Item and Transaction.
Table Item
ID Name
1 Copper Wire
2 Bolt
3 Screw
4 Rubber Band
5 Pipe
Table Transaction
Trans_ID Item_ID Qty
T1 1 1
T1 2 2
T1 3 1
T1 4 2
T2 1 1
T2 3 2
I needs a select query showing a table like this
ID Name Trans_ID QTY
1 Copper Wire t2 1
2 Bolt
3 Screw t2 2
4 Rubber Band
5 Pipe
Is is possible to do it using MySql Select query alone?
Implicit join:
SELECT Item.ID, Name, Trans_ID, QTY
FROM Item, Transaction
WHERE Item.ID=Transaction.ID
(this query will not have the items without transaction)
Or explicit:
SELECT Item.ID, Name, Trans_ID, QTY
FROM Item
LEFT JOIN Transaction
ON Item.ID=Transaction.ID
(this one will, since it is a LEFT JOIN and the "left" is the Item table)