mysql DELETE rows when multiple conditions are met - mysql

So, I have following wpdb DELETE function:
$wpdb->delete( $table, array( 'sub_id' => $sub_id ) ????);
I want to delete rows where WHERE two conditions are met
For example, when sub_id = $sub_id AND date = $date, then I want to delete a row where these two conditions are available.
How do I change above function?
Thanks

You can go in this direction:
$wpdb->query("DELETE FROM table_name WHERE 'sub_id'=".$sub_id." AND date=".$date);

If you want to use multiple where conditions, than the second parameter should be a associative array. The conditions will be joined with "AND".
$wpdb->delete($wpdb->prefix . 'table_name', array('sub_id' => $sub_id, 'date' => $date));
Following query will be generated:
DELETE FROM wp_table_name WHERE sub_id = 1 AND date = 2017-07-17 00:00:00
For more info look here: DELETE Rows

Related

Laravel query builder not returning the full row if it does not find a join with another table

Here, my currently working query is
$results = DB::table('amenities as a')
->leftJoin('amenity_values as av','av.amenity_id','=','a.id')
->leftJoin('units_amenities_values as uav','uav.amenity_value_id','=','av.id')
->leftJoin('units as u','uav.unit_id','=','u.id')
->leftJoin('amenity_pricing_reviews as apr','apr.unit_id','=','u.id')
->select('a.id as amenity_id','a.amenity_name','a.category_id', 'av.amenity_value','u.id as unit_id','u.unit_number','apr.dom','apr.building_id')
->where('a.property_id', $data['property_id'])
->whereIn('apr.building_id', $data['building_ids'])
->whereNull('apr.deleted_at')
->whereNull('u.deleted_at')
->whereNull('av.deleted_at')
->whereNull('a.deleted_at')
->orderBy('a.amenity_name','asc')
->get()
->groupBy(['category_id','unit_id']);
Here, I have joined to a table amenity_pricing_reviews. In this relationship whenever it could not find the related rows in the amenity_pricing_reviews table it is discarding the full rows:
However, I want to get these rows with an empty value for those columns from that table. Something like:
amenity_id => value
amenity_name => value
category_id => value
amenity_value => value
unit_id => value
unit_number => value
dom =>
building_id =>
Also, I have done a lot of other things based on the result of this query so I want to make minimal changes to the structure of the end result.
Your whereIn condition can't succeed if the amenity pricing review doesn't exist, so it effectively changes your left joins into inner joins. Perhaps you want that to be just a condition of the join?
->leftJoin('amenity_pricing_reviews as apr', function($join) { $join->on('apr.unit_id','=','u.id')->whereIn('apr.building_id', $data['building_ids']) })
Try join instead of leftJoin. This should help

Sort array in PHP or MySQL

i have a problem.
i want to sort array follow max value in colum feed_id with every user_id,
then loop.
this is array start :
$array_start = array(
array("user_id"=>1,"feed_id"=>10),
array("user_id"=>1,"feed_id"=>11),
array("feed_id"=>2,"user_id"=>25),
array("feed_id"=>9,"user_id"=>26),
array("feed_id"=>2,"user_id"=>30),
array("feed_id"=>7,"user_id"=>33),
);
then sort array_start, i want to return array_result :
$array_result = array(
//loop 1
array("user_id"=>1,"feed_id"=>37),
array("feed_id"=>2,"user_id"=>30),
array("feed_id"=>9,"user_id"=>26),
array("feed_id"=>7,"user_id"=>33),
//loop 2
array("user_id"=>1,"feed_id"=>11),
array("feed_id"=>2,"user_id"=>25),
);
please help me, thank your reading !
Here you go!
I am writing the query based on a random table name. Please change it accordingly.
select user_id, max(feed_id) from MyTable
group by user_id
order by feed_id DESC;
The above query will result both the columns and the sorting will be done on feed_id and in descending order.

Yii2 : LeftJoin query with Where clause & orderBy clause

I have write query to get distinct stateID whose status is active, from tbl_summer table which is primary key of table tbl_states.
I want the listing of distinct state names in alphabetical order.
Actually i got this from following query but alphabetical order is not getting...
So what is the solution...?
Here is my query :
$query = Tbl_summer::find()
->select('tbl_summer.StateID, tbl_states.state_name')
->distinct('tbl_summer.StateID')
->from('tbl_summer')
->leftJoin('tbl_states', ['tbl_states.ID' => 'tbl_summer.StateID'])
->where(['tbl_summer.IsActive' => '1'])
->orderBy(['tbl_states.state_name' => SORT_ASC]);
Does this work?
$query = Tbl_summer::find()
->select('tbl_summer.StateID, tbl_states.state_name')
->from('tbl_summer')
->leftJoin('tbl_states', ['tbl_states.ID' => 'tbl_summer.StateID'])
->where(['tbl_summer.IsActive' => '1'])
->groupBy('tbl_summer.StateID, tbl_states.state_name')
->orderBy(['tbl_states.state_name' => SORT_ASC]);
I think the second field in groupBy is not needed if there is only one name for one id.

Doctrine orWhere for multiple columns

I need to fetch using doctrine, my entity has three date values, expiryDate, scheduledDate, addedDate(mandatory). I need to check first is there any row having scheduledDate as today , If no then I need to check for entry before expiryDate which has the latest addedDate.
I try this code ,
public function getLatestEntry(){
$today = date("Y-m-d");
$qb = $this->em->createQueryBuilder();
$qb ->select('MyEntry')
->from('MyEntry', 'myEntry')
->Where('myEntry.scheduledOn = :today')
->orWhere('myEntry.expirationDate >= :today')
->orWhere('myEntry.expirationDate is null')
->orderby('myEntry.addedDate')
->setMaxResults(1)
->setParameters(array('today' => $today));
return $qb->getQuery()->getResult();
}
But I didn't get the result I expected ,I feel like the first where condition has been ignored, How can I tackle this?
Right now you have 3 conditions with equal priority. If one of these is true the row is fetched. But you want your conditions with different priorities. Either the first one matches or it does not match and second matches. Not that your third condition is part of your second one:
$qb ->select('MyEntry')
->from('MyEntry', 'myEntry')
->Where('myEntry.scheduledOn = :today')
->orWhere('(myEntry.scheduledOn != :today AND (myEntry.expirationDate >= :today OR myEntry.expirationDate IS NULL))')
->orderby('myEntry.addedDate')
->setMaxResults(1)
->setParameters(array('today' => $today));
EDIT:
I'm not quite shure if you want to have entries with expiration date NULL equally treated as the ones with an expiration date set. If its not equal for you, you could alter your ORDER BY clause like
->orderby('myEntry.addedDate', 'ASC')
->addOrderBy('my.Entry.expirationDate', 'DESC')

How to get the last updated row using a single query

I searched enough before posting it.
My table structure:
aid | bid | cid | did |
Where aid, bid together are the primary keys.
When I update the value of cid using a where clause for aid, bid I also want to get the did value of the updated row.
Something like this:
$this->db->set('cid', 1, FALSE)
->where(array(
'aid' => $a_id,
'bid' => $b_id
))
->update('my_table')
->select('did');
The above query says:
Fatal error: Call to a member function select() on a non-object in...
I tried this:
How to get ID of the last updated row in MySQL?
Which is like 3 queries.
I'd suggest fetching the values you're about to update, store their IDs in an array, and run an UPDATE with a WHERE id IN (1, 2, ...).
What you're trying to do is not supported by MySQL. You'll need to run at least 2 queries, and since you're fetching the values the first time and already know what values you're updating, then you can also recreate the new row and it's values without using a query after UPDATE.
In your given example:
$this->db->set('cid', 1, FALSE)
->where(array(
'aid' => $a_id,
'bid' => $b_id
))
->update('my_table')
->select('did');
set(), where() and also select() returns an object that builds on the query. However update() return a value which is the results and doesn't have a function called select() and not set() and where() for that matter.