MySQL: fill table column from other column with pivot table - mysql

I have 3 tables, "questions", "q_t" -> pivot table and "tags":
I need to fill tags.lid with first occurrence in q_t.tid to be = questions.lid
"questions" table structure:
qid lid
1 901
2 901
3 500
4 500
5 200
6 210
7 333
8 423
"q_t" table structure:
qid tid
1 8
2 4
3 8
4 1
1 2
6 3
2 8
8 1
"tags" table structure:
tid lid
1 null
2 null
3 null
4 null
5 null
6 null
7 null
8 null
I need "tags" table to be:
tid lid
1 500
2 901
3 210
4 901
5 null
6 null
7 null
8 901
thanks,

I would go with a query like this:
UPDATE tags t
SET t.lid = (
SELECT q.lid
FROM questions q
JOIN q_t ON (q.qid = q_t.qid)
WHERE q_t.tid = t.tid
LIMIT 1
)
It might not be the best performance-wise, but for a one-time job, it should do.
As an alternative:
UPDATE tags t
JOIN (
SELECT q.lid, q_t.tid
FROM questions q
JOIN q_t USING (qid)
GROUP BY q_t.tid
) d USING(tid)
SET t.lid = d.lid

Related

select user whose child is less than 2

i have table structure like this
id
user_id
parent_id
club
1
1
club1
2
2
1
club1
3
3
1
club1
4
4
2
club1
5
5
2
club1
6
6
3
club1
7
7
3
club1
8
8
4
club1
9
9
4
club1
10
10
5
club1
11
11
5
club1
12
12
6
club1
13
13
6
club1
14
14
7
club1
15
15
7
club1
i want to select user_id whose child is less then 2.
refer to above table user 1,2,3,4,5,6 and 7 two child complete so the query should return 8,9,10,11,12,13,14,15
Refer to above table here user_id 1 have two child 2 and 3 , user_id 2 have child 4 and 5 and so on.
i need to select user_id whose child is less than 2
You can do it as follows :
SELECT user_id
FROM club_tree
WHERE user_id NOT IN (
SELECT parent_id
from club_tree
where club = 'club1'
group by parent_id
HAVING count(1) >= 2
)
and club = 'club1';
We Select users that are not in the list of users with more than two childs.
This is to get users with more than two childs :
SELECT parent_id
from club_tree
group by parent_id
HAVING count(1) >= 2
Here you can test it : http://sqlfiddle.com/#!9/eb2c70/3
Can you try this query using left join :
SELECT c.user_id
from club_tree c
left join
(
select parent_id
from club_tree
where club = 'club1'
group by parent_id
HAVING count(1) >= 2
) as c2 on c.user_id = c2.parent_id
where c.club = 'club1' and c2.parent_id is null;
Try it here : http://sqlfiddle.com/#!9/eb2c70/17

Self-referential table ordering in SQL

Given a table like this:
taskId
nextTaskId
1
2
2
3
3
6
4
5
5
NULL
6
7
7
8
8
4
I need the following order for output:
taskId
nextTaskId
1
2
2
3
3
6
6
7
7
8
8
4
4
5
5
NULL
Is there any way to do this via MySQL query?
Using recursive CTE -
WITH RECURSIVE
self_ref_cte ( taskid, nexttaskid )
AS
( SELECT taskid, nexttaskid
FROM self_ref WHERE taskid = 1
UNION ALL
SELECT s.taskid, s.nexttaskid
FROM self_ref_cte c JOIN self_ref s
ON s.taskid = c.nexttaskid
)
SELECT * FROM self_ref_cte;
DB fiddle here.

Need to find count of shipment id which corresponds to certain set of order id in sql table but query is buggy

I have sales_order table
entity_id state created_at
1 success 2020-05-01
2 fail 2020-05-01
3 success 2020-05-01
4 success 2020-05-01
5 process 2020-05-01
6 process 2020-05-01
7 process 2020-05-01
8 fail 2020-05-01
9 fail 2020-05-01
And a sales_shipment table
entity_id order_id
1 1
2 1
3 2
4 2
5 3
6 4
7 4
8 4
9 5
10 6
11 6
12 6
13 6
14 7
15 7
16 8
17 9
18 9
19 9
20 9
I wanted to find out what is the count of entity_id from sales_shipment table which corresponds to success and process state orders from sales_order table
Expected output
Count
13
When I run the following query I am not getting the desired output
select `entity_id` as tem, count(*) as cntsid
from `sales_order` a
inner join `sales_shipment` b on a.`entity_id`= b.`order_id`
where (`state` = 'success' or `state` = 'process')
group by tem
Instead I am getting
Column 'entity_id' in field list is ambiguous
Please help with this query
You need to prepend entity_id with the table name or alias in the first line, in this case I added a.. Otherwise the engine doesn't know which one to use (there are two entity_id columns in the query).
Your query should look like:
select a.`entity_id` as tem, count(*) as cntsid
from `sales_order` a
inner join `sales_shipment` b on a.`entity_id`= b.`order_id`
where (b.`state` = 'success' or b.`state` = 'process')
group by tem

Select record from table with not in other table

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)

Mysql left join or much simpler way?

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.