Mysql concat select and update - mysql

I have two queries:
1) $query = "SELECT `login`, `password` FROM `TEST_ACCS` WHERE `sold` = '0' LIMIT 3";
And then I need change sold to 1 from the result of first query
2) $setQuery = "UPDATE `chrome_ext`.`TEST_ACCS` SET `sold` = '1' WHERE `sold` = '0' LIMIT 3";
How to concat these two queries into one?
I tried
UPDATE `chrome_ext`.`TEST_ACCS` dest, (SELECT `login`, `password` FROM `TEST_ACCS` WHERE `sold` = '0' LIMIT 3) src SET dest.sold = '1' where dest.sold= '0'
But it set sold = 1 to all raws, but need just 3

Related

SUMs with GROUP WITH ROLLUP - query too slow

My Query is very slow returning results. It takes more than 30s (depending a lot on the use of the server). The table has some records.
I cannot divide this table for other reasons. I added this to the query `TYPE` IN (1,3)' and the response time has improved significantly. But it remains very slow.
It is worth mentioning that I have already add an INDEX for the PRODUCT, TYPE, STATUS columns.
Question: How can I speed up the query response time without having to divide it by many tabs and have to use the INNER JOIN?
Other Useful Information:
Records: 1,436,004
Record length: 240 Bytes
Records size: 291 Bytes
My Query:
SELECT `PRODUCT`,
SUM(DATE_FORMAT(`REG_DATE`,'%m/%Y') = '11/2020' AND `TYPE` = 1) AS `SUMT1_1`,
SUM(DATE_FORMAT(`REG_DATE`,'%m/%Y') = '12/2020' AND `TYPE` = 1) AS `SUMT1_2`,
SUM(DATE_FORMAT(`REG_DATE`,'%m/%Y') = '01/2021' AND `TYPE` = 1) AS `SUMT1_3`,
SUM(DATE_FORMAT(`REG_DATE`,'%m/%Y') = '02/2021' AND `TYPE` = 1) AS `SUMT1_4`,
SUM(DATE_FORMAT(`REG_DATE`,'%m/%Y') = '03/2021' AND `TYPE` = 1) AS `SUMT1_5`,
SUM(DATE_FORMAT(`REG_DATE`,'%m/%Y') = '04/2021' AND `TYPE` = 1) AS `SUMT1_6`,
SUM(DATE_FORMAT(`REG_DATE`,'%m/%Y') = '11/2020' AND `TYPE` = 3) AS `SUMT3_1`,
SUM(DATE_FORMAT(`REG_DATE`,'%m/%Y') = '12/2020' AND `TYPE` = 3) AS `SUMT3_2`,
SUM(DATE_FORMAT(`REG_DATE`,'%m/%Y') = '01/2021' AND `TYPE` = 3) AS `SUMT3_3`,
SUM(DATE_FORMAT(`REG_DATE`,'%m/%Y') = '02/2021' AND `TYPE` = 3) AS `SUMT3_4`,
SUM(DATE_FORMAT(`REG_DATE`,'%m/%Y') = '03/2021' AND `TYPE` = 3) AS `SUMT3_5`,
SUM(DATE_FORMAT(`REG_DATE`,'%m/%Y') = '04/2021' AND `TYPE` = 3) AS `SUMT3_6`
FROM `MY_TABLE` WHERE
`TYPE` IN (1,3) AND
`STATUS` IN('AVAILABLE','IN PROCESS')
GROUP BY `PRODUCT` ASC
WITH ROLLUP HAVING (`SUMT1_1`+`SUMT1_2`+`SUMT1_3`+`SUMT1_4`+`SUMT1_5`+`SUMT1_6`+`SUMT3_1`+`SUMT3_2`+`SUMT3_3`+`SUMT3_4`+`SUMT3_5`+`SUMT3_6`) >= 1
With an index on some combination of (status,type,reg_date), I would write that query this way:
SELECT product
, DATE_FORMAT(reg_date,'%Y-%m') reg_ym
, type
, COUNT(*) subtotal
FROM my_table
WHERE type IN (1,3)
AND status IN('AVAILABLE','IN PROCESS')
AND reg_date >= '2020-11-01'
AND reg_date < '2020-05-01'
GROUP
BY product
, reg_ym
, type
ORDER
BY product
, reg_ym
, type
...and handle any remaining aspects of the problem in application code
The best solution was given by #Akina (in the comments), and reduced the response time from 30s to 3s (average).
Adding the condition:
`REG_DATE` BETWEEN '20220-11-01' AND '2021-04-30'
But to fit better with my php code i prefer to use:
DATE_FORMAT(`REG_DATE`,'%m/%Y') IN ('11/2020','12/2020','01/2021','02/2021','03/2021','04/2021')

UPDATE Query WHERE conditions not working in millisecond differences

I have working Update query
UPDATE contact
SET ownerid = '1548'
, alloted = '1'
, genby = '1548'
, leadstatus = '-34-,'
, callbackdate = '2019-09-25'
, read = '0'
WHERE id = '22484307'
AND alloted = '0'
AND leadsource = '29'
AND `delete` = '0'
AND ownerid = '0'
AND oldlead != '1'
but when this query is updated by two users at the same time (approx) WHERE condition stops working
2019-09-25T09:12:27.211547+05:30 9688978 Query UPDATE `contact` SET `ownerid` = '1548',`alloted` = '1',`genby` = '1548', `leadstatus` = '-34-,', `callbackdate` = '2019-09-25', `read` = '0' WHERE `id` = '22484307' AND `alloted` = '0' AND `leadsource` = '29' AND `delete` = '0' AND `ownerid` = '0' AND `oldlead` != '1'
2019-09-25T09:12:27.400647+05:30 9689052 Query UPDATE `contact` SET `ownerid` = '1535',`alloted` = '1',`genby` = '1535', `leadstatus` = '-34-,', `callbackdate` = '2019-09-25', `read` = '0' WHERE `id` = '22484307' AND `alloted` = '0' AND `leadsource` = '29' AND `delete` = '0' AND `ownerid` = '0' AND `oldlead` != '1'
It's updating again with the same id and replacing data with millisec difference here is log
full screenshot
I was only checking query execution status not affected status adding that resolved issue thanks all for time mysqli_affected_rows($con) thank you #Solarflare
$updateStatus=mysqli_query($con,"UPDATE `contact` SET `ownerid` = '$loggeduserid',`alloted` = '1',`genby` = '$loggeduserid', `leadstatus` = '-34-,', `callbackdate` = '$date', `read` = '0' WHERE `id` = '$row[0]' AND `alloted` = '0' AND `leadsource` = '$leadS' AND `delete` = '0' AND `ownerid` = '0' AND `oldlead` != '1'") or die(mysqli_error($con));
if(mysqli_affected_rows($con) AND $updateStatus)
{
}

Mysql query in gas ORM

this is i am getting
SELECT * FROM (`lkt_messages`) WHERE `sender_id` = '1' AND `receiver_id` = '2' OR `sender_id` = '2' OR `receiver_id` = '1' ORDER BY `added_on` DESC
I want like this
SELECT * FROM (`lkt_messages`) WHERE `sender_id` = '1' AND `receiver_id` = '2' OR `sender_id` = '2' AND `receiver_id` = '1' ORDER BY `added_on` DESC
I write the query below.
$cls=MESSAGES_TBL;
$id= $this->session->userdata('user_id');
$condition_query=array('sender_id'=>$replymsg_id,'receiver_id'=>$id);
$condition_query1=array('sender_id'=>$id,'receiver_id'=>$replymsg_id);
$data=$cls::where($condition_query)->or_where($condition_query1);
$result=$data->order_by('added_on', 'DESC')->all();
return $result;
Following change should work:
$condition_query = "(sender_id = $replymsg_id and receiver_id = $id)";
$condition_query1 = "(sender_id = $id and receiver_id = $replymsg_id)";
$data = $cls::where($condition_query)->or_where($condition_query1);
Try this sql query:-
SELECT *
FROM (`lkt_messages`)
WHERE (`sender_id` = '1' AND `receiver_id` = '2')
OR (`sender_id = '2' AND `receiver_id` = '1')
ORDER BY `added_on` DESC

list of several names, to update their rows in the table with same information

Can somebody refresh my memory on how to build a query for this.
I want to use a list of several names (first and last), to update their rows in the table with same information. for example:
if I have a table set up with the columns:
[first_name],[last_name],[dob],[married_status]
I want to find:
(bob, smith),
(robert, john),
(jane, doe);
and edit their field for [married_status] to 'm'.
how do I structure this search and replace?
Thanks!
Use the IN operator:
UPDATE myTable
SET married_status = 'm'
WHERE (first_name, last_name) IN (
('bob' , 'smith'),
('robert', 'john'),
('jane' , 'doe'))
Code:
UPDATE tablename
SET married_status = 'm'
WHERE
( first_name = 'bob' AND last_name = 'smith' )
OR
( first_name = 'robert' AND last_name = 'john' )
OR
( first_name = 'jane' AND last_name = 'doe' )
You would use UPDATE query:
UPDATE `table`
SET `married_status` = 'm'
WHERE
(`first_name` = 'bob' AND `last_name` = 'smith')
OR (`first_name` = 'robert' AND `last_name` = 'john')
OR (`first_name` = 'jane' AND `last_name` = 'doe')

Finding difference resullts in subquery returns more than 1 row in mysql error

I have a table named tbl_populations having following fields:
pk_populationid,residence,value,aspect,gender,fk_tbl_states_stateid
I am trying to calculate the difference of rows for two aspects
for e.g.
SELECT fk_tbl_states_stateid, value - (
SELECT value
FROM tbl_populations
WHERE fk_tbl_states_stateid = '16'
AND fk_tbl_districts_districtid = '0'
AND residence = '0'
AND gender = '0'
AND aspect = '2' ) AS difference
FROM tbl_populations
WHERE fk_tbl_states_stateid = '16'
AND fk_tbl_districts_districtid = '0'
AND residence = '0'
AND gender = '0'
AND aspect = '1'
It is working fine as it returns one row
For fetching multiple data that is i want to retrieve all gender values , i have removed the gender from the condition.
SELECT fk_tbl_states_stateid,gender, value - (
SELECT value
FROM tbl_populations
WHERE fk_tbl_states_stateid = '16'
AND fk_tbl_districts_districtid = '0'
AND residence = '0'
AND aspect = '2' ) AS difference
FROM tbl_populations
WHERE fk_tbl_states_stateid = '16'
AND fk_tbl_districts_districtid = '0'
AND residence = '0'
AND aspect = '1'
I am getting Subquery returns more than 1 row error.
How can i get the all results?
SELECT Table11.fk_tbl_states_stateid,Table1.Gender,Table1.value - table2.Value as Diff
From
(SELECT *
FROM tbl_populations
WHERE fk_tbl_states_stateid = '16'
AND fk_tbl_districts_districtid = '0'
AND residence = '0'
AND aspect = '2' ) Table1,
(SELECT value,Gender
FROM tbl_populations
WHERE fk_tbl_states_stateid = '16'
AND fk_tbl_districts_districtid = '0'
AND residence = '0'
AND aspect = '1' ) Table2
Where Table1.Gender = Table2.Gender
use a function for it , should be easy that way