Mysql query in gas ORM - mysql

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

Related

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)
{
}

sql query search by anchored field

Help me plz, this sql not workin
SELECT *
FROM p_pl
WHERE (`sid` = '25' AND `value` = 'zxc')
AND (`sшd` = '22' AND `value` = 'cxz')
this sql workin:
SELECT * FROM p_pl WHERE (`sid` = '25' AND `value` = 'zxc')
How make query? thanks
Maybe you want OR ?
SELECT *
FROM p_pl
WHERE (`sid` = '25' AND `value` = 'zxc')
OR (`sid` = '22' AND `value` = 'cxz')
I think you want to use OR:
select *
from p_pl
where (`sid` = '25' and `value` = 'zxc')
or (`sid` = '22' and `value` = 'cxz')
Or simply:
select *
from p_pl
where (sid, value) in(('25','zxc'),('22','cxz'));
Should you be using 'OR' instead of 'AND'?
SELECT *
FROM p_pl
WHERE (`sid` = '25' AND `value` = 'zxc')
OR(`sшd` = '22' AND `value` = 'cxz')

How to get if else statement in mysql query in WHERE clause?

There is I am doing mistake in my mysql query. can anyone help me out??
select * from user_errors where user_id = '2'
IF( type = 'custom_type' ) second_user_id != '2' ELSE second_user_id = '2'
END IF
There is no necessary to use if else, anyway try this;)
select * from user_errors
where user_id = '2'
and ((type = 'custom_type' and second_user_id != '2') or second_user_id = '2')
Or you must use it, try this;)
select * from user_errors
where user_id = '2'
and case when type = 'custom_type' then second_user_id != '2' else second_user_id = '2' end
Use like this( You don't need to use if else condition)
select * from user_errors where user_id = '2'
and ((type = 'custom_type' and second_user_id != '2')
or (type != 'custom_type' and second_user_id = '2'))
try this
select * from user_errors
where user_id = '2'
IF(type = 'custom_type', second_user_id != '2', second_user_id = '2' )
SELECT * FROM user_errors
WHERE user_id = '2'AND ( case when type = 'custom_type' then second_user_id != '2' else second_user_id = '2' end)

Mysql concat select and update

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

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