Woocommerce Select Orders which match multiple meta values - php

I'm currently working on a plugin which was built for a bakery to email reports about orders. They have since opened another store and now the reports need to be split into separate emails depending on store location.
I have the following sql query:
$sql = "SELECT o.order_item_id, o.order_item_name, o.order_id, po.post_date AS order_date_placed
FROM wp_woocommerce_order_items AS o
LEFT JOIN wp_woocommerce_order_itemmeta AS oi ON o.order_item_id = oi.order_item_id
LEFT JOIN wp_posts AS po ON po.ID = o.order_id
WHERE oi.meta_key='Collection Date'
AND oi.meta_value IN ('16/01/2016','17/01/2016','18/01/2016','19/01/2016','20/01/2016','21/01/2016')
ORDER BY po.post_date ASC, o.order_item_id ASC";
This query returns all orders which are to be collected within a specific date range.
array (size=3)
0 =>
object(stdClass)[593]
public 'order_item_id' => string '19582' (length=5)
public 'order_item_name' => string '10 Chocolate Cupcakes' (length=21)
public 'order_id' => string '15234' (length=5)
public 'order_date_placed' => string '2016-01-14 08:51:31' (length=19)
1 =>
object(stdClass)[592]
public 'order_item_id' => string '19600' (length=5)
public 'order_item_name' => string '10 Chocolate Cupcakes' (length=21)
public 'order_id' => string '15248' (length=5)
public 'order_date_placed' => string '2016-01-14 15:43:08' (length=19)
2 =>
object(stdClass)[591]
public 'order_item_id' => string '19603' (length=5)
public 'order_item_name' => string '10 Chocolate Cupcakes' (length=21)
public 'order_id' => string '15250' (length=5)
public 'order_date_placed' => string '2016-01-14 15:45:25' (length=19)
I now want to pass an additional criteria that will also limit the return data to a specific store location.
I have tried adding the following to the query but this returns null.
WHERE oi.meta_key = 'Pickup Location'
AND oi.meta_value LIKE '%Address 1%'
Only records which are true for both criteria must return.
I do battle to understand why the meta table in wordpress/woocommerce are set up the way they are with so many redundant/duplicate entries.
I'm not sure whether the developer should have used a raw SQL query or whether there is a better solution for this problem.
Any help is much appreciated.

I guess you are looking for this query
SELECT o.order_item_id,
o.order_item_name,
o.order_id,
po.post_date AS order_date_placed
FROM wp_woocommerce_order_items AS o
LEFT JOIN wp_woocommerce_order_itemmeta AS oi
ON ( o.order_item_id = oi.order_item_id
AND oi.meta_key = 'Collection Date'
AND oi.meta_value IN ( '16/01/2016', '17/01/2016','18/01/2016',
'19/01/2016','20/01/2016', '21/01/2016' ) )
OR ( oi.meta_key = 'Pickup Location'
AND oi.meta_value LIKE '%Address 1%' )
LEFT JOIN wp_posts AS po
ON po.id = o.order_id
ORDER BY po.post_date ASC,
o.order_item_id ASC
And move the filter to ON conditions since those belongs to right table

Related

Query with union condition

Does anyone know how can I write a query to get the desired result to be like tools/lift planning/cranimax from this database
resource_category_id category_name parent_category
1 Tools 0
2 product literature 0
3 Terms and Conitions 0
4 crane library 1
5 geniune 1
6 lift planning 1
8 cranimax 6
For this I wrote a query like this which is not completed, for example this is thing am having in my mind if I choose from cranimax it's having a parent_category 6 so it should make union with that row having resource_category_id and that row having parent_category 1 it should repeat until parent category 0 occurs.
$data['breadcrumbs'] = $this->Manito_model->get_breadcrumbs_details($resource_id);
public function get_breadcrumbs_details($resource_id)
{
$query=$this->db->query("select * from resource_category as m WHERE m.resource_category_id = $resource_id union (select * from resource_category where parent_category != 0) as m2 on m.parent_category = m2.resource_parent_category");
return $query->result();
}
i expect my result to be like this
array (size=3)
0 =>
object(stdClass)[35]
public 'resource_category_id' => string '8' (length=1)
public 'category_name' => string 'cranimax' (length=8)
public 'parent_category' => string '6' (length=1)
public 'created_at' => string '2017-11-04 13:59:39' (length=19)
1 =>
object(stdClass)[35]
public 'resource_category_id' => string '6' (length=1)
public 'category_name' => string 'lift planning' (length=8)
public 'parent_category' => string '1' (length=1)
public 'created_at' => string '2017-11-04 13:59:39' (length=19)
2 =>
object(stdClass)[35]
public 'resource_category_id' => string '1' (length=1)
public 'category_name' => string 'Tools' (length=8)
public 'parent_category' => string '0' (length=1)
public 'created_at' => string '2017-11-04 13:59:39' (length=19)
UNION is used to combine the result from multiple SELECT statements into a single result set (ie making it extremely simple: you need to do two select, but you want only one result, then you make a union)
In you situation you don't need a UNION, but a simple JOIN which "complete" your results with other data:
select m.*, m2.category_name AS parent_name FROM resource_category AS m LEFT JOIN resource_category m2 ON m2.resource_category_id = m.resource_parent_category WHERE m.resource_category_id = $resource_id

Inner joins SQL in WP environment with where id =id

Hello friend I have a problem. My id is '28' and I want to show only where has this id, its has first object. How can I make this type of SQL?
WP sql:
$rows = $wpdb->get_results(
"SELECT hotels.id,hotels.hotel,reviews.hotel_id,reviews.id
FROM hotels
INNER JOIN reviews ON hotels.id = reviews.hotel_id" );
var_dump($rows);
var_dump
array (size=4)
0 =>
object(stdClass)[710]
public 'id' => string '28' (length=2)
public 'hotel' => string 'xxxxx' (length=14)
public 'hotel_id' => string '17' (length=2)
1 =>
object(stdClass)[709]
public 'id' => string '29' (length=2)
public 'hotel' => string 'xxxxxx' (length=14)
public 'hotel_id' => string '17' (length=2)
Use WHERE clause
SELECT hotels.hotel,reviews.hotel_id,reviews.id
FROM hotels
INNER JOIN reviews ON hotels.id = reviews.hotel_id
WHERE reviews.id = 28

Codeigniter $query->result() returns strange results

This peace of code is driving me crazy for last hour...
I have this model which should return all non active records from DB
$query = $this->db->get($tableName);
echo $this->db->last_query();
var_dump($query->result());
$this->db->last_query() output is
SELECT * FROM `locations` LEFT JOIN `provinces` ON `provinces`.`id_province` = `locations`.`id_province` LEFT JOIN `countries` ON `countries`.`id_country` = `provinces`.`id_country` LEFT JOIN `statuses` ON `statuses`.`id_status` = `locations`.`id_status` WHERE `locations`.`active` = '0' ORDER BY `locations`.`id_location` DESC LIMIT 50
If i run exactsame query in phpmyadmin i get correct results
But when i var_dump data var_dump($query->result()); i get the following results
array (size=50)
0 =>
object(stdClass)[61]
public 'unique_id' => string 'OWYwYjBmNm' (length=10)
public 'active' => string '1' (length=1)
public 'owner_name' => string 'Cleve Greenfelder' (length=17)
1 =>
object(stdClass)[62]
public 'unique_id' => string 'YWY4YmMzMm' (length=10)
public 'active' => string '1' (length=1)
public 'owner_name' => string 'Bradford Hyatt' (length=14)
Why/how this active state get's overwritten from 0 to 1?
IF you need any additional information's, please let me know and i will provide. Thank you!
Once i wrote a question, answer quickly appeared in my head :)
Table Countries has also active field, so this field overwrites active state as enabled. I needed to specified fields and there's name in query in order to get proper results
$fields = 'unique_id, locations.active as active, ....';

SQL JOIN returns query with wrong ids for each row

In my database I have a table named posts and a table named topics.
Each post has a parent topic.
In this case posts.parent_topic is a foreign key to the associated topics.id
I am attemping to do a JOIN so that my query will return the correct topics.topic_title from the topics table. I have been able to successfully do this, however I am noticing that I am no longer getting the correct posts.id for anything that is returned.
My query:
$posts = $db->query("
SELECT
*
FROM
posts
JOIN topics ON
posts.post_parent_topic = topics.id
ORDER BY
posts.created_on DESC
")->fetchALL(PDO::FETCH_ASSOC);
After doing a var_dump on the $posts variable I am seeing these results:
array (size=2)
0 =>
array (size=12)
'id' => string '1' (length=1) // this id is CORRECT for the post
'post_parent_topic' => string '1' (length=1)
'created_on' => string '2015-11-03 09:30:40' (length=19)
'post_title' => string 'Development Standards' (length=21)
'post_content' => string 'These are the development specifc standards.' (length=44)
'topic_title' => string 'Code Standards' (length=14)
'topic_content' => string 'These are the company programming standards.' (length=44)
'topic_parent_organization' => string '1' (length=1)
1 =>
array (size=12)
'id' => string '1' (length=1) // this id is INCORRECT for the post, it should be an id of 2 (as it appears in the database)
'post_parent_topic' => string '1' (length=1)
'created_on' => string '2015-11-03 09:30:40' (length=19)
'post_title' => string 'Design Standards' (length=16)
'post_content' => string 'These are the design specific standards.' (length=40)
'topic_title' => string 'Code Standards' (length=14)
'topic_content' => string 'These are the company programming standards.' (length=44)
'topic_parent_organization' => string '1' (length=1)
Why would each post return the same id? I need the id of the post as it is in my database.
If you have id column in both posts and topics tables, you may need to give them aliases to make it unambigious:
Try changing the query to the following:
SELECT p.*, t.id AS TopicId, t.topic_title, t.topic_content, t.topic_parent_organization
FROM posts AS p
JOIN topics AS t
ON posps.post_parent_topic = t.id
ORDER BY p.created_on DESC

Selecting unique records from mysql with considering only one field

My question may be not reflecting my problem, sorry for that at first. I have a problem , I need to select distinct movie names from a table also selecting its id and other records. But since ids are different for each movies, all the movies are selected(with same name ). Here's the query :
$sql = "
SELECT DISTINCT
movie_id,movie_name
FROM
tbl_current_movies as cm, tbl_movie_hall as mh
WHERE
movie_active = 'active'
AND
cm.hall_id = mh.hall_id
";
$res = $this->db->returnArrayOfObject($sql,$pgin = 'no');
var_dump($res);
And var_dump($res) says :
array
0 =>
object(stdClass)[48]
public 'movie_id' => string '1' (length=1)
public 'movie_name' => string 'MIB' (length=12)
1 =>
object(stdClass)[49]
public 'movie_id' => string '2' (length=1)
public 'movie_name' => string 'Jetuka Pator Dare' (length=17)
2 =>
object(stdClass)[50]
public 'movie_id' => string '3' (length=1)
public 'movie_name' => string 'MIB' (length=12)
So as you can see the movie MIB is showing twice, but I want to get in the results the movies MIB only once !
Change your query to this :
$sql = "SELECT movie_id , movie_name
FROM tbl_current_movies as cm
LEFT JOIN tbl_movie_hall mh ON cm.hall_id = mh.hall_id
WHERE movie_active = 'active'
GROUP BY movie_name
";
You shouldn't join your tables in the WHERE clause, if you're not confortable with the JOINs you can read some of the docs : http://dev.mysql.com/doc/refman/5.0/en/join.html
That should help you understand what they are for.

Categories