How to optimize a UNION mysql query with multiple select queries? - php

I've a requirement to show the similar products in product detail page in such a way that the products listed should match the current product properties in a hierarchical way as given below
Size, color, category, company should match with the current product
Size, color, category should match with the current product
Size, color should match with the current product
Size should match with the current product
My sql query is as given below:
(SELECT pd.product_id, pd.name, p.price
FROM mg_product_description pd
JOIN `mg_product` p ON p.product_id = pd.product_id
WHERE
pd.size_id = '33' AND
pd.color_id = '2' AND
pd.category_id = '3' AND
pd.company_id = '1' AND
pd.product_id != '53' AND
p.status = '1'
ORDER BY RAND() LIMIT 10
)
UNION
(SELECT pd.product_id, pd.name, p.price
FROM mg_product_description pd
JOIN `mg_product` p ON p.product_id = pd.product_id
WHERE
pd.size_id = '33' AND
pd.color_id = '2' AND
pd.category_id = '3' AND
pd.product_id != '53' AND
p.status = '1'
ORDER BY RAND() LIMIT 10
)
UNION
(SELECT pd.product_id, pd.name, p.price
FROM mg_product_description pd
JOIN `mg_product` p ON p.product_id = pd.product_id
WHERE
pd.size_id = '33' AND
pd.color_id = '2' AND
pd.product_id != '53' AND
p.status = '1'
ORDER BY RAND() LIMIT 10
)
UNION
(SELECT pd.product_id, pd.name, p.price
FROM mg_product_description pd
JOIN `mg_product` p ON p.product_id = pd.product_id
WHERE
pd.size_id = '33' AND
pd.product_id != '53' AND
p.status = '1'
ORDER BY RAND() LIMIT 10
)
53 - current product id and status denotes available!
Is there any way to optimize the above query?
Note Output required: We need 10 similar products. If there exists 4 products matching condition 1, then they need to listed at first in random order. Similarly we need to list the products matching other conditions below it.
Thanks in Advance!

Keeping using ORDER BY RAND() (which is not efficient) then a very basic improvement would be to add a priority for each clause, move the order clause to the end so it is only needed once.
(SELECT pd.product_id, pd.name, p.price , 1 AS recpriority
FROM mg_product_description pd
JOIN `mg_product` p ON p.product_id = pd.product_id
WHERE
pd.size_id = '33' AND
pd.color_id = '2' AND
pd.category_id = '3' AND
pd.company_id = '1' AND
pd.product_id != '53' AND
p.status = '1'
)
UNION
(SELECT pd.product_id, pd.name, p.price , 2 AS recpriority
FROM mg_product_description pd
JOIN `mg_product` p ON p.product_id = pd.product_id
WHERE
pd.size_id = '33' AND
pd.color_id = '2' AND
pd.category_id = '3' AND
pd.product_id != '53' AND
p.status = '1'
)
UNION
(SELECT pd.product_id, pd.name, p.price , 3 AS recpriority
FROM mg_product_description pd
JOIN `mg_product` p ON p.product_id = pd.product_id
WHERE
pd.size_id = '33' AND
pd.color_id = '2' AND
pd.product_id != '53' AND
p.status = '1'
)
UNION
(SELECT pd.product_id, pd.name, p.price , 4 AS recpriority
FROM mg_product_description pd
JOIN `mg_product` p ON p.product_id = pd.product_id
WHERE
pd.size_id = '33' AND
pd.product_id != '53' AND
p.status = '1'
)
ORDER BY recpriority, RAND() LIMIT 10
That could then be done without the need for unions by doing something like this:-
SELECT pd.product_id,
pd.name,
p.price ,
CASE
WHEN pd.color_id = '2' AND pd.category_id = '3' AND pd.company_id = '1' THEN 1
WHEN pd.color_id = '2' AND pd.category_id = '3' THEN 2
WHEN pd.color_id = '2' THEN 3
ELSE 4
END AS recpriority
FROM mg_product_description pd
JOIN `mg_product` p ON p.product_id = pd.product_id
WHERE pd.size_id = '33'
AND pd.product_id != '53'
AND p.status = '1'
ORDER BY recpriority, RAND()
LIMIT 10

Related

Select from 6 tables get one result only

I have about 6 tables and I am while looping on them the results comes 1 result it should be more
see my code here
$institute = $_POST['institute'];
$sections = $_POST['sections'];
$division = $_POST['division'];
$getSearch = $db->prepare("SELECT
a.name, a.phase, a.setNumber, a.email, a.sudImage, a.activity, a.id AS stud_id,
b.id, b.ins_name,
c.id, c.sec_name,
d.id, d.div_name,
e.id, e.std_id, e.sub_id, e.absence,
f.id, f.sub_name, f.subHour, f.level
FROM student_basic_info AS a
JOIN institutes AS b ON (a.institute = b.id)
CROSS JOIN ins_sections AS c ON (a.section = c.id)
CROSS JOIN ins_division AS d ON (a.division = d.id)
CROSS JOIN student_absence AS e ON (a.id = e.std_id)
CROSS JOIN ins_subjects AS f ON (e.sub_id = f.id)
WHERE a.institute =? AND a.section = ? AND a.division =?
GROUP BY a.id
");
$studSearch = array();
$getSearch->bind_param('iii', $institute, $sections, $division);
if ($getSearch->execute()) {
$results = $getSearch->get_result();
while ($vStud = mysqli_fetch_assoc($results)) {
$studSearch[] = $vStud;
var_dump($studSearch);
?>
the var_dump
array (size=1)
0 =>
array (size=17)
'name' => string 'الدفع النقدي' (length=23)
'phase' => string 'ابتدائي' (length=14)
'setNumber' => int 0
'email' => string 'johnef' (length=6)
'sudImage' => string '' (length=0)
'activity' => int 0
'stud_id' => int 7
'id' => int 3
'ins_name' => string 'معهد الفنون' (length=21)
'sec_name' => string 'فنون مسرح' (length=17)
'div_name' => string 'شعبة مرجانية 10' (length=26)
'std_id' => int 7
'sub_id' => int 3
'absence' => string '' (length=0)
'sub_name' => string 'فنون تطبيقية' (length=23)
'subHour' => string '2' (length=1)
'level' => string 'المستوي الأول' (length=25)
EDIT
SELECT a.name, a.phase, a.setNumber, a.email, a.sudImage, a.activity,
a.id AS stud_id, b.id, b.ins_name, c.id, c.sec_name, d.id, d.div_name,
e.id, e.std_id, e.sub_id, e.absence, f.id, f.sub_name, f.subHour, f.level
FROM student_basic_info AS a
JOIN institutes AS b ON (a.institute = b.id)
CROSS JOIN ins_sections AS c ON (a.section = c.id)
CROSS JOIN ins_division AS d ON (a.division = d.id)
CROSS JOIN student_absence AS e ON (a.id = e.std_id)
CROSS JOIN ins_subjects AS f ON (e.sub_id = f.id)
WHERE a.institute =1 AND a.section = 1 AND a.division =7
GROUP BY a.id
and yes I got the same results (1) record cam out which it should be at last (6) or (7) of records

MySql query result to have nested arrays

I just want my query to return a result looking like this:
array(
[k1] => data1
[k2] => data2
[items] => array(
[item1] => array(
.... data inside
)
[item2] => array(
.... data inside
)
)
)
Here is my query:
SELECT o.*
, ot.tracking_number
, p.id payId
, p.customer_id payCustId
, p.name payName
, p.status payStatus
, p.charge payTotalCharge
, op.*
FROM orders o
JOIN payments o
ON p.id = o.payment_id
LEFT
JOIN orders_tracking ot
ON ot.order_id = o.id
JOIN orderdetails od
ON od.order_id = o.id
JOIN orderproducts op
ON op.orderdetail_id = od.id
WHERE p.customer_id = $customer_id;
What is wrong with my query that it won't return the way I wanted it to?
Thanks.

mysql combine two select queries

I know this can be confusing, but please bear with me on this one.
I have two SELECT queries with a minor difference which returns pretty much the same result set.
SELECT products_id,options_values_id
FROM products_attributes pa
LEFT JOIN products_options po ON ( pa.options_id = po.products_options_id )
WHERE products_id ='574' and pa.options_id!=6
and pa.options_id!=3
AND products_options_type = 6
GROUP BY products_id,options_values_id
ORDER BY products_id,products_options_sort_order,options_id
The second query varies at products_options_type
SELECT products_id,options_values_id
FROM products_attributes pa
LEFT JOIN products_options po ON ( pa.options_id = po.products_options_id )
WHERE products_id ='574' and pa.options_id!=6
and pa.options_id!=3
AND products_options_type = 2
GROUP BY products_id,options_values_id
ORDER BY products_id,products_options_sort_order,options_id
And the results returned by them are
574|193
574|204
AND
574|25
574|3
I want the output as
574|193|25
574|204|3
What I tried is:
SELECT pa.products_id,pa.options_values_id,ord.options_values_id
FROM products_attributes pa
LEFT JOIN products_options po ON ( pa.options_id = po.products_options_id )
LEFT JOIN(SELECT products_id,options_values_id
FROM products_attributes pa
LEFT JOIN products_options po ON ( pa.options_id = po.products_options_id )
WHERE products_id ='574' and pa.options_id!=6
and pa.options_id!=3
AND products_options_type = 2
GROUP BY products_id,options_values_id
ORDER BY products_id,products_options_sort_order,options_id)ord ON pa.products_id=ord.products_id
WHERE paproducts_id ='574' and pa.options_id!=6
and pa.options_id!=3
AND products_options_type = 2
GROUP BY pa.products_id,pa.options_values_id
ORDER BY pa.products_id,products_options_sort_order,options_id
However this returns
574|193|25
574|204|25
I am not too good with joins, so any idea if and how this can be done?
Try this
SELECT a.products_id,a.options_values_id, b.options_values_id
FROM (SELECT #rownum:=#rownum+1 'rw', products_id, options_values_id
FROM (SELECT #rownum:=0) r, products_attributes pa
LEFT JOIN products_options po ON ( pa.options_id = po.products_options_id )
WHERE products_id ='574' and pa.options_id!=6
and pa.options_id!=3
AND products_options_type = 6
GROUP BY products_id,options_values_id
ORDER BY products_id,products_options_sort_order,options_id) a,
(SELECT #rownum:=#rownum+1 'rw', products_id,options_values_id
FROM (SELECT #rownum:=0) r, products_attributes pa
LEFT JOIN products_options po ON ( pa.options_id = po.products_options_id )
WHERE products_id ='574' and pa.options_id!=6
and pa.options_id!=3
AND products_options_type = 2
GROUP BY products_id,options_values_id
ORDER BY products_id,products_options_sort_order,options_id) b
WHERE a.products_id=b.products_id and a.rw=b.rw;
Hope this helps.
Try below query, here i took join on product_options twice, 1 for po.products_options_type = 6 and 2nd for po1.products_options_type = 2
SELECT pa.products_id,po.options_values_id,po1.options_values_id
FROM products_attributes pa
LEFT JOIN products_options po ON ( pa.options_id = po.products_options_id and po.products_options_type = 6)
LEFT JOIN products_options po1 ON ( pa.options_id = po1.products_options_id and po.products_options_type = 2)
WHERE pa.products_id ='574' and pa.options_id!=6
and pa.options_id!=3
GROUP BY products_id,po1.options_values_id,po1.options_values_id
ORDER BY products_id,products_options_sort_order,options_id
Alternative solution:
select v1,v2,v3, v4 from (SELECT products_id v1,options_values_id v2
FROM products_attributes pa
LEFT JOIN products_options po ON ( pa.options_id = po.products_options_id )
WHERE products_id ='574' and pa.options_id!=6
and pa.options_id!=3
AND products_options_type = 6
GROUP BY products_id,options_values_id
ORDER BY products_id,products_options_sort_order,options_id) t1,
(SELECT products_id v3,options_values_i v4
FROM products_attributes pa
LEFT JOIN products_options po ON ( pa.options_id = po.products_options_id )
WHERE products_id ='574' and pa.options_id!=6
and pa.options_id!=3
AND products_options_type = 2
GROUP BY products_id,options_values_id
ORDER BY products_id,products_options_sort_order,options_id) t2
where t1.products_id=t2.products_id
You can sort it easily.

get all tags with sql query

I have this sql that is made by help of others.
$sql ="
select e.*, i.*, group_concat( t.tag separator ',') as tag_list
from nv_entries e
JOIN nv_tags t on t.entrie_id = e.id
LEFT JOIN nv_images i on i.entrie_id = e.id
where t.tag in ( $tag_list )
group by e.id
having count(t.id) = $num_tags ";
The result is this (i only show one entrie here, could be more):
[1] => Array
(
[id] => 2
[band] => Kids for Cash
[album] => No More Walls E.P.
[label] =>
[year] => 1986
[text] => Text about album kids for cash.
[entrie_id] => 2
[source] => img02_9lch1.png
[tag_list] => tree
)
For the tags, i have to show all tags that a entrie has and highlight the tags that where used to get the result. In this case [tag_list] => tree only shows one tag, the one that was used in the search field. My question is, how can i get a result like this?:
...
[tag_list] => tree, green, foo, bar
[used_tags] => tree
)
As a array is also good, but then please also an array when it's just one item.
If I understood correctly use >= in the having condition
$sql ="
select e.*, i.*, group_concat( t.tag separator ',') as tag_list
from nv_entries e
LEFT JOIN nv_images i on i.entrie_id = e.id
JOIN nv_tags t on t.entrie_id = e.id
where t.tag in ( $tag_list )
group by e.id
having count(t.id) >= $num_tags ";
ADD
subquery approach:
$sql ="
select e.*, i.*, group_concat( t.tag separator ',') as tag_list
from nv_entries e
JOIN nv_tags t on t.entrie_id in (
select se.id
from nv_entries se
JOIN nv_tags st on st.entrie_id = se.id
where st.tag in ( $tag_list )
group by se.id
having count(st.id) >= $num_tags
)
LEFT JOIN nv_images i on i.entrie_id = e.id
WHERE 1
group by e.id
";
Into subquery I get the ID list of entrie havin at least requested tags, then in main query I get all infox
ADD fixed query (see asker comment)
subquery approach, fix the lost join between "e" and "t" :
$sql ="
select e.*, i.*, group_concat( t.tag separator ',') as tag_list
from nv_entries e
JOIN nv_tags t on t.entrie_id = e.id
LEFT JOIN nv_images i on i.entrie_id = e.id
WHERE e.id in (
select se.id
from nv_entries se
JOIN nv_tags st on st.entrie_id = se.id
where st.tag in ( $tag_list )
group by se.id
having count(st.id) >= $num_tags
)
group by e.id
";

SQL & PHP query with union

I have this query string...
select i.invoiceid, i.date, i.total, i.total - (select ifnull(sum(p.amount), 0) from payment p where p.invoice = i.invoiceid) as remainingbalance
from invoice i inner join client c
on i.client = c.clientid
where i.isdeleted = 0 and i.client = 1
union
select p.paymentid, p.date, p.invoice, p.amount from payment p inner join invoice i
on i.invoiceid = p.invoice
inner join paymenttype
on paymenttype.paymenttypeid = p.paymenttypeid
inner join client c
on c.clientid = i.client
where c.clientid = 1
and i.isdeleted = 0
order by date
when I try this...
<?php
echo $clientArrayInvoice[1]['paymentid'];
?>
I get no results when I do a print_r on on $clientArrayInvoice I get this
Array ( [0] => Array ( [invoiceid] => 1 [date] => 2012-04-12 [total] => 602.29 [remainingbalance] => 300.96 ) [1] => Array ( [invoiceid] => 1 [date] => 2012-04-27 [total] => 1.00 [remainingbalance] => 301.33 ) )
I understand why its doing this, but how do I get the column to say paymentid instead of invoiceid for the results returned. so I can determine what is a paymentid and what is a invoiceid I hope this makes sense.
select i.invoiceid as transactionid, i.date, i.total,
i.total - (select ifnull(sum(p.amount), 0) from payment p where p.invoice = i.invoiceid) as remainingbalance,
'invoice' as transaction_type
from invoice i inner join client c
on i.client = c.clientid
where i.isdeleted = 0 and i.client = 1
union
select p.paymentid as transactionid, p.date, p.invoice, p.amount, 'payment' as transaction_type
from payment p inner join invoice i
on i.invoiceid = p.invoice
inner join paymenttype
on paymenttype.paymenttypeid = p.paymenttypeid
inner join client c
on c.clientid = i.client
where c.clientid = 1
and i.isdeleted = 0
order by date

Categories