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)
{
}
Related
I am writing a query in mysql i am not familer with the recursive query to much.
how and what i need to do to optimization of the below query as the conditions i put not looks good ,there must be a easier way to do same.
select
b.entity_id
from
entity_hierarchies a,
entity_hierarchies b
where
a.entity_id = 25
and a.entity_type = 'user'
and b.entity_type = 'idea'
and a.Geography_Geography =
case
when
a.Geography_Geography is null
then
a.Geography_Geography
else
b.Geography_Geography
end
and COALESCE(a.Geography_Country, '') =
case
when
a.Geography_Country is null
then
COALESCE(a.Geography_Country, '')
else
b.Geography_Country
end
and COALESCE(a.Geography_DistrictOrCounty, '') =
case
when
a.Geography_DistrictOrCounty is null
then
COALESCE(a.Geography_DistrictOrCounty, '')
else
b.Geography_DistrictOrCounty
end
and COALESCE(a.Geography_State, '') =
case
when
a.Geography_State is null
then
COALESCE(a.Geography_State, '')
else
b.Geography_State
end
and COALESCE(a.Geography_City, '') =
case
when
a.Geography_City is null
then
COALESCE(a.Geography_City, '')
else
b.Geography_City
end
Intro
I noticed you could rewrite some of these statements in a much simpler form.
So for example:
and a.Geography_Geography =
case
when
a.Geography_Geography is null
then
a.Geography_Geography
else
b.Geography_Geography
end
can simply become:
AND ( a.Geography_Geography is null or a.Geography_Geography = b.Geography_Geography )
Final Solution
select
b.entity_id
from
entity_hierarchies a,
entity_hierarchies b
where
a.entity_id = 25
and a.entity_type = 'user'
and b.entity_type = 'idea'
AND ( a.Geography_Geography is null OR a.Geography_Geography = b.Geography_Geography )
AND ( a.Geography_Country is null OR a.Geography_Geography = b.Geography_Country )
AND ( a.Geography_DistrictOrCounty is null OR a.Geography_DistrictOrCounty = b.Geography_DistrictOrCounty )
AND ( a.Geography_State is null OR a.Geography_State = b.Geography_State )
AND ( a.Geography_City is null OR a.Geography_City = b.Geography_City );
I have the following query which gives me the overall status of a project. I was wondering if there was a better way to write the same query.
an example schema for the table:
CREATE TABLE `test_status`
(
`test_id` int
(11) NOT NULL,
`group_id` int
(11) NOT NULL,
`sub_test_id` int
(11) NOT NULL,
`project_id` int
(11) NOT NULL,
`collection_status` varchar
(20) DEFAULT NULL,
`labeling_status` varchar
(20) DEFAULT NULL,
`upload_status` varchar
(20) DEFAULT NULL,
`upload_date` date DEFAULT NULL,
`disk_number` int
(11) DEFAULT NULL,
`subject_id` varchar
(10) DEFAULT NULL,
`collection_id` varchar
(25) DEFAULT NULL,
`assigned_to` int
(11) DEFAULT NULL,
`assigned_on` date DEFAULT NULL,
`turned_in_date` date DEFAULT NULL,
PRIMARY KEY
(`test_id`,`group_id`,`sub_test_id`,`project_id`));
My Query:
select
(select count(*)
from test_status
where project_id = 7 and collection_status = 'COLLECTED') as collected,
(select count(*)
from test_status
where project_id = 7 and collection_status = 'SCHEDULED') as scheduled,
(select count(*)
from test_status
where project_id = 7 and collection_status = 'CORRUPTED') as corrupted,
(select count(*)
from test_status
where project_id = 7 and collection_status is NULL) as 'not collected',
(select count(*)
from test_status
where project_id = 7 and ((labeling_status = 'GOOD' and (upload_status != 'UPLOADED' or upload_status != 'QUEUED')) or (labeling_status = 'Labeled'))) as 'Labeled',
(select count(*)
from test_status
where project_id = 7 and (labeling_status = 'RAW') or (labeling_status = 'ASSIGNED') ) as 'to be labeled',
(select count(*)
from test_status
where project_id = 7 and labeling_status = 'RE-LABEL') as 'Re-label',
(select count(*)
from test_status
where project_id = 7 and (upload_status = 'UPLOADED' or upload_status = 'QUEUED')) as 'Uploaded',
(select count(*)
from test_status
where project_id = 7 and labeling_status = 'GOOD' and upload_status is null) as 'ready to be uploaded';
You can try to use condition aggregate function will be better.
let the condition in CASE WHEN
SELECT
COUNT(CASE WHEN collection_status = 'COLLECTED' THEN 1 END),
COUNT(CASE WHEN collection_status = 'SCHEDULED' THEN 1 END),
COUNT(CASE WHEN collection_status = 'CORRUPTED' THEN 1 END),
COUNT(CASE WHEN collection_status is NULL THEN 1 END),
COUNT(CASE WHEN ((labeling_status = 'GOOD' and (upload_status != 'UPLOADED' or upload_status != 'QUEUED')) or (labeling_status = 'Labeled'))THEN 1 END),
COUNT(CASE WHEN (labeling_status = 'RAW') or (labeling_status = 'ASSIGNED') THEN 1 END),
COUNT(CASE WHEN labeling_status = 'RE-LABEL' THEN 1 END),
COUNT(CASE WHEN (upload_status = 'UPLOADED' or upload_status = 'QUEUED') THEN 1 END),
COUNT(CASE WHEN labeling_status = 'GOOD' and upload_status is null THEN 1 END)
FROM test_status
WHERE project_id = 7
Or you can try a simpler way, SUM with bool (0 or 1) to count
SELECT
SUM(collection_status = 'COLLECTED'),
SUM(collection_status = 'SCHEDULED'),
SUM(collection_status = 'CORRUPTED' ),
SUM(collection_status is NULL ),
SUM(((labeling_status = 'GOOD' and (upload_status != 'UPLOADED' or upload_status != 'QUEUED')) or (labeling_status = 'Labeled'))),
SUM((labeling_status = 'RAW') or (labeling_status = 'ASSIGNED') ),
SUM(labeling_status = 'RE-LABEL' ),
SUM((upload_status = 'UPLOADED' or upload_status = 'QUEUED')),
SUM(labeling_status = 'GOOD' and upload_status is null )
FROM test_status
WHERE project_id = 7
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
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
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