Update table column on the basis of another table column - php

I have 2 tables. 'report' and 'panel_calculation' are the tables.
report contains:
r_zone r_address r_status
=======+==========+============
1 8 0
2 9 0
2 6 0
7 9 0
3 2 0
panel_calculation contains:
p_cal_zone p_cal_address p_status
===========+==============+============
7 9 1
3 2 1
I need to update the r_status column in 'report' table on the basis of 'panel_calculation' table.
so that the final result would be like this:
Final 'report' should be like this:
r_zone r_address r_status
=======+==========+============
1 8 0
2 9 0
2 6 0
7 9 1
3 2 1
I need your suggestion.

you could still join both table even when updating the table.
UPDATE report s
INNER JOIN panel_calculation b
ON s.r_zone = b.p_cal_zone
SET s.r_status = b.p_status
SQLFiddle Demonstration

Try this:
UPDATE report r
INNER JOIN panel_calculation p ON r.r_zone = p.p_cal_zone
SET r.r_status = p.p_status;
SQL Fiddle Demo

As a solution to your problem please try executing below sql query
update report r set r_status=(select p_status from panel_calculation where p_cal_zone=r.r_zone limit 1 )

You can use LEFT JOIN like this:
UPDATE report t1
LEFT JOIN panel_calculation t2
ON t1.r_zone=t2.p_cal_zone
AND t1.r_address=t2.p_cal_address
SET t1.r_status=t2.p_status

Related

Count pass, fail, skip number according to id using PHP and MySQL

I have four tables look like below:
test_case (1='pass',2='fail',3='skip')
id testing_status
1 1
2 3
3 1
4 2
5 3
6 2
7 2
8 3
test_suite_with_case (referenced with test_case_id in test_case table and test_suite_id with test_suite table)
id test_suite_id test_case_id
1 4 1,3,5,2,6,7,8
2 3 2,5,4,6,7
test_suite
id test_suite_name
3 test_1
4 test_2
test_suite_run (referenced with test_suite_id in test_suite table)
id test_suite_id name
1 3 BBH
2 4 CXN
Now i want to run a where query from test_suite_run by id (for example id=2 in test_suite_run) and want a output look like below:
id(test_suite_run) name test_suite_name pass fail skip
1 CXN test_suite_2 2 2 3
I am new in PHP and MySQL.
Try this:
select
tr.id,
tr.name,
ts.test_suite_name,
sum(testing_status = 1) pass,
sum(testing_status = 2) fail,
sum(testing_status = 3) skip
from test_suite_run tr
inner join test_suite ts on tr.test_suite_id = ts.id
inner join test_suite_with_case tsc on ts.id = tsc.test_suite_id
inner join test_case tc on find_in_set(tc.id, tsc.test_case_id) > 0
group by
tr.id,
tr.name,
ts.test_suite_name;

Mysql tree traverse and count

I`ve got the following table in mysql
id|top_id|amount
1 NULL 2
2 NULL 8
3 NULL 4
4 3 7
5 2 8
6 2 4
7 5 5
8 7 1
9 6 6
10 8 6
For the first 3 ids i need to sum up all amounts of its successors to get the following:
id | amount
1 8
2 32
3 11
I guess there should be joins but unfortunately I can`t get the working mysql request. Can somebody help me?
UPD: In php I have the following query:
$tops = $mysqli->query('SELECT * FROM table WHERE top_id IS NULL')
which obviously returns me just 3 top ids and their plain amounts (2, 8 and 4 respectively). Instead of 2, 8 and 4 I need to get 12, 28 and 11 and that`s the problem((
This is the crudest solution... No doubt someone will provide some hints for a more scalable solution shortly (perhaps employing PHP)...
SELECT a.id
, COALESCE(a.amount,0)
+ COALESCE(b.amount,0)
+ COALESCE(c.amount,0)
+ COALESCE(d.amount,0)
+ COALESCE(e.amount,0)
+ COALESCE(f.amount,0) total
FROM my_table a
LEFT
JOIN my_table b
ON b.top_id = a.id
LEFT
JOIN my_table c
ON c.top_id = b.id
LEFT
JOIN my_table d
ON d.top_id = c.id
LEFT
JOIN my_table e
ON e.top_id = d.id
LEFT
JOIN my_table f
ON f.top_id = e.id
WHERE a.top_id IS NULL;

get count number of records which respectively exist in another table with null value also

i have two tables. 'listing_messages' and 'listing_message_history'.
listing_messages
=============
message_id listing_id listing_user third_party_user
1 162 7 32
2 162 7 33
3 162 7 12
listing_message_history
======================
listing_message_history message_id is_checked
1 1 0
2 1 1
3 1 1
4 2 0
5 2 1
6 3 0
Search criteria==> listing_user=7, is_checked=1
I want result..
message_id count_of_unread_message_history
12
2 1
3 0
I have made a query is
SELECT count(`lmh`.`message_history_id`) AS COUNT,
`lmh`.`message_id`
FROM `listing_message_history` AS `lmh`
LEFT JOIN `listing_messages` AS `lm` ON `lmh`.`message_id` = `lm`.`message_id`
WHERE `lm`.`third_party_user`=7
AND `lmh`.is_checked=1
GROUP BY `lm`.`message_id`
but it does not returns message_id with count = 0 which message_id do not have is_checked=1
In general, when you use left join, filter conditions on the second table should go in the on clause not the where clause. Try this:
SELECT count(`lmh`.`message_history_id`) AS COUNT,
`lmh`.`message_id`
FROM `listing_message_history` `lmh` LEFT JOIN
`listing_messages` `lm`
ON `lmh`.`message_id` = `lm`.`message_id` AND
`lm`.`third_party_user` = 7
WHERE `lmh`.is_checked = 1
GROUP BY `lmh`.`message_id`;
In addition, you should use the first table for the group by, not the second table.

multiple column update with the value of other column [duplicate]

This question already has an answer here:
UPDATE all column values equivalent to another tables column value based on their id
(1 answer)
Closed 8 years ago.
i have two tables
fw_invitations
id moduleid qdetailid
1 1 10
2 2 11
3 3 12
4 7 13
5 8 14
fw_ind_details
id moduleid officeid
10 0 100
11 0 110
12 0 120
13 0 130
14 0 104
the column qdetaild is the primary key in fw_ind_details.what i want to do is want to take the moduleid from fw_invitations table for that particular qdetailid and want to update in fw_ind_details.that is it should be done with an update query.
desired output
fw_ind_details
id moduleid officeid
10 1 100
11 2 110
12 3 120
13 7 130
14 8 104
this is what i tried so far;
UPDATE `fw_ind_details`
SET `moduleid`=(select moduleid
from fw_invitaions fo,fw_ind_details fi
where f0.qdetailid=fi.id
)
i know my approach is wrong,any suggestion will be appreciated.
I am open for the use of PHP.
UPDATE fw_ind_details d
JOIN fw_invitations i ON i.qdetailid = d.id
SET d.moduleid = i.moduleid
To get a particular module ID when there are multiple matches, you can join with a subquery; this example uses the highest module ID:
UPDATE fw_ind_details d
JOIN (SELECT qdetailid, MAX(moduleid) moduleid
FROM fw_invitations
GROUP BY qdetailid) i
ON i.qdetailid = d.id
SET d.moduleid = i.moduleid
Use a join...
UPDATE `fw_ind_details`
JOIN fw_invitations ON fw_ind_details.id = fw_invitations.qdetailid
SET fw_ind_details.moduleid = fw_invitations.moduleid
The SQL to use is:
UPDATE `fw_ind_details` fi
SET `moduleid`=(SELECT moduleid
FROM fw_invitaions fo
WHERE fo. qdetailid =fi.id
)

get SUM of another row of GROUP BY'd rows

I have this table:
This selection is is duplicated many times for different var_lines (which pretty much work as one row of data, or respondent for a survey) and set_codes (different survey codes).
With this query:
SELECT
*, COUNT(*) AS total
FROM
`data`
WHERE
`var_name` = 'GND.NEWS.INT'
AND(
`set_code` = 'BAN11A-GND'
OR `set_code` = 'BAN09A-GND'
OR `set_code` = 'ALG11A-GND'
)
AND `country_id` = '5'
GROUP BY
`data_content`,
`set_code`
ORDER BY
`set_code`,
`data_content`
The query basically counts the number of answers for a specific question. Then groups them survey (set_code).
What I need is for each of the grouped data_content answers for GND.NEWS.INT to also show the SUM of all the corresponding GND_WT with the same var_line.
For example if I had this:
data_id data_content var_name var_line
1 2 GND.NEW.INT 1
2 1.4 GND_WT 1
3 2 GND.NEW.INT 2
4 1.6 GND_WT 2
5 3 GND.NEW.INT 3
6 0.6 GND_WT 3
I would get something like this:
data_id data_content var_name var_line total weight
1 2 GND.NEW.INT 1 2 3
5 3 GND.NEW.INT 3 1 0.6
Thanks for any help.
Your requirements are not exactly clear, but I think the following gives you what you want:
select d1.data_id,
d1.data_content,
d1.var_name,
d1.var_line,
t.total,
w.weight
from data d1
inner join
(
select data_content,
count(data_content) Total
from data
group by data_content
) t
on d1.data_content = t.data_content
inner join
(
select var_line,
sum(case when var_name = 'GND_WT' then data_content end) weight
from data
group by var_line
) w
on d1.var_line = w.var_line
where d1.var_name = 'GND.NEW.INT'
See SQL Fiddle with Demo
This Query can be suitable for your specific example:
select st.data_id,
st.data_content,
st.var_name,
st.var_line,
count(st.data_id) as total,
sum(st1.data_content) as weight
from data st
left join data st1 on st1.var_name = 'GND_WT' AND st1.var_line=st.var_line
where st.var_name='GND.NEW.INT'
group by st.data_content
Regards,
Luis.

Categories