I have table A:
id name product_id shipped
1 Apple 10 100
2 Orange 11 110
3 Banana 12 0
4 Mango 13 0
And Table B:
id product_id qty order_id
1 10 100 2
2 11 110 2
3 12 120 3
4 13 130 2
I need to update the quantity
UPDATE table_B SET qty=0 WHERE order_id=2
AND table_B.product_id = table_A.product_id
The table_A.product_id is get from a script. How can I do this? Thank you
UPDATE table_B b
LEFT JOIN table_A a
ON b.product_id = a.product_id
SET b.qty= 0 WHERE b.order_id=2
Related
I have a table1 for example:
orderID userId orderName orderTime...
1 11
2. 12
3. 11
4. 14
5. 11
6. 13
7. 11
8. 15
9. 16
10. 11
... ...
I have another table table2:
table2ID orderID item price ....
101 1 Apple 1.99
102 1 Banana 2.99
103 1 Grapes 0.99
104 4 pizza 6.99
105 4 drink 0.99
105. 3 chicken 1.99
106. 3 apple 1.99
I have tried this :
SELECT a.*, b.* FROM `table1` a
RIGHT JOIN table2 b on a.orderID = b.orderID
WHERE a.userID = 11 order by a.`orderTime` DESC LIMIT 25;
I want to get upto 10 unique orderIDs from table 1 of user 11 and all the details of that 10 ids from table 2. If I do LIMIT 25 then I don't get all the information.
I want my output as:
orderID userId orderName orderTime... table2ID orderID item price
1 11 101 1 Apple 1.99
1 11 102 1 Banana 2.99
1 11 103 1 Grapes 0.99
3 11 105 3 chicken 1.99
3 11 106 3 apple 1.99
SELECT a.*, b.*
FROM ( SELECT *
FROM a
WHERE a.userID = 11
LIMIT 10) as a
JOIN b
ON a.orderID = b.orderID
I am stuck in query I have a table like this
budget_details
id budget_id expenditure_head_id budget
1 1 1 1233
2 1 2 333
3 1 3 567
4 2 1 343
5 2 2 343
6 2 3 6767
7 2 4 557
expenditure_heads
id name
1 abc
2 xyz
3 qwe
4 uvw
I want to get all the expenditure_heads from budget_details that even
if not in budget_details like here budget_id=1 does not contain expenditure_head_id 4
but I want to select that to with 0 or null displaying
I tried this but it not displaying expenditure_head_id 4
select `expenditure_heads`.`name`, `budget_details`.`budget`, `budget_details`.`id` from
`budget_details`
right join `expenditure_heads` on `budget_details`.`expenditure_head_id` = `expenditure_heads`.`id`
where `budget_details`.`budget_id` = 1"
The where avoid you to get the missing row you need. The left join is done on the ON statement, so this query should work for you:
SELECT EH.name, BD.budget, BD.id FROM expenditure_heads EH
LEFT JOIN budget_details BD
ON (BD.expenditure_head_id = EH.id AND BD.budget_id = 1)
I have a table that is having 3 columns
vid - auto increment column
video_id - containing numbers
a_id - containing junk numbers
The table looks like below.
Vid Video_id a_id
101 1 3
102 1 3
103 5 3
104 5 3
105 5 3
106 11 3
107 11 3
108 11 3
109 11 3
110 11 3
I want to update a_id column values based on video_id values. Values in a_id should be updated as below.ex: If there are five 11 digit in video_id then the value in a_id should be updated 1 through 5.
Vid Video_id a_id
101 1 1
102 1 2
103 5 1
104 5 2
105 5 3
106 11 1
107 11 2
108 11 3
109 11 4
110 11 5
You can use user defined variables to give rank for each video group and then join with your real table by your auto increment column and update a_id accordingly
update t
join (
SELECT
Vid,
#r:= CASE WHEN Video_id = #g THEN #r+1 ELSE #r:=1 END a_id
,#g:=Video_id
FROM t,(SELECT #r:=0,#g:=0) t1
ORDER BY Video_id
) t1
on(t.Vid =t1.Vid)
set t.a_id = t1.a_id
Demo
I'll try to explain it as simple as possible:
First some database structure with dummy data.
Structure
tb_spec_fk
feature value
-----------------
1 1
1 2
1 3
1 4
1 5
2 2
2 3
3 1
3 4
4 2
4 3
4 4
5 1
5 3
5 5
6 3
6 5
tb_spec_feature
feature_id filter
------------------
1 2
2 2
3 2
4 2
5 1
6 0
tb_spec_value
value_id name
----------------
1 10
2 20
3 30
4 40
5 50
Now, what I want is the follow result
Result
feature_id min_value max_value
---------------------------------
1 10 50
2 20 30
3 10 40
4 20 40
But how?
Logic
Get from the tb_spec_feature where "filter" equals 2 the highest and lowest values which are present in the tb_spec_value table and connected together trough the tb_spec_fk table.
My attemps
A lot! But I'll spare you :)
SELECT
f.feature_id AS feature_id,
MAX(value.name) AS max_value,
MIN(value.name) AS min_value
FROM tb_spec_feature AS f
JOIN tb_spec_fk AS fk ON f.feature_id=fk.feature
JOIN tb_spec_value AS value ON fk.value=value.id
WHERE f.filter=2
GROUP BY f.feature_id
The two JOIN statements "link" the a feature to a value. GROUP BY groups all rows with the same feature id, and then you can take the min or max or any other aggregate function on those columns.
Demo
Here is how you can do it
select
tsf.feature_id,
tsvl.name as Min_Value,
tsvr.name as Max_Value
from tb_spec_feature as tsf
inner join (select feature , MIN(value) MinV,MAX(value)MaxV from tb_spec_fk group by feature order by feature)as tsfkl on tsfkl.feature = tsf.feature_id
left join tb_spec_value as tsvl on tsvl.value_id = tsfkl.MinV
left join tb_spec_value as tsvr on tsvr.value_id = tsfkl.MaxV
where tsf.filter = 2
group by tsf.feature_id
Output
feature_id | Min_Value | Max_Value
---------------------------------
1 | 10 | 50
2 | 20 | 30
3 | 10 | 40
4 | 20 | 40
Fiddle Demo
I need to order data according to order index from other table. and order the data with the same 'id' according to entry date.
i cant figure it out how to join data and order using mysql command.
Table1
id name order
1 Ali 1
2 Cenk 3
3 Tan 2
Table 2
id tid m date
1 232 msj1 3
2 434 msj2 2
1 453 msj4 1
3 455 msj5 2
2 541 msj6 4
1 234 msj7 2
3 132 msj8 6
Needed query result
id tid m date
1 453 msj4 1
1 234 msj7 2
1 232 msj1 3
3 455 msj5 2
3 132 msj8 6
2 434 msj2 2
2 541 msj6 4
This should work:
select t2.id, t2.tid, t2.m, t2.date
from t2
left join t1 on t2.id=t1.id
order by t1.order
This orders by the ordering field from table 1.