How to get multidimensional array from yii 'CDbDataReader::readAll' - php

It is two tables in a database. The first tablse contains a list of products, and the second contains a list of options. For one product may be several options. I have built next query with yii query bulder
SELECT DISTINCT `p`.`id`,
`p`.`name`,
`pc`.`category_id`,
`c`.`name` AS `category_name`,
`ov`.`value_str` AS `option_value`
FROM `cms_product` `p`
LEFT JOIN `cms_product_category` `pc` ON p.id = pc.product_id
LEFT JOIN `cms_category` `c` ON c.id = pc.category_id
LEFT JOIN `cms_product_option_set` `pos` ON p.id = pos.product_id
LEFT JOIN `cms_option_variant` `ov` ON ov.id=pos.option_variant_id
ORDER BY `p`.`id` ASC LIMIT 100
But yii function 'CDbDataReader::readAll()' returns only one option for each product. If I execute this query in MySQL it works good. How I can get right result with yii

Use queryAll() method like below:
$sql="SELECT DISTINCT `p`.`id`,
`p`.`name`,
`pc`.`category_id`,
`c`.`name` AS `category_name`,
`ov`.`value_str` AS `option_value`
FROM `cms_product` `p`
LEFT JOIN `cms_product_category` `pc` ON p.id = pc.product_id
LEFT JOIN `cms_category` `c` ON c.id = pc.category_id
LEFT JOIN `cms_product_option_set` `pos` ON p.id = pos.product_id
LEFT JOIN `cms_option_variant` `ov` ON ov.id=pos.option_variant_id
ORDER BY `p`.`id` ASC LIMIT 100";
Then:
$result=Yii::app()->db->createCommand($sql)->queryAll();
This will return you an array with the result.

Related

Slow mysql query using Doctrine

To filter the products that belongs to the selected filters, I wrote this query below. When i run this in Mysql, it is very fast. But when i'm trying to execute this query in Doctrine, it is very slow. Removing the group by gives me one result and than it is fast, but it has to be 11 results.
Even the more filters i add to this statement, the slower it gets.
Can anyone help me out with this problem?
SELECT DISTINCT p.id, p.brand_id, p.name, p.permalink, p.base_model,
p.ean, p.description, p.unit, p.price, p.offer_active, p.offer_price,
GROUP_CONCAT(DISTINCT m.url ORDER BY `m`.`order`, `p`.`id` SEPARATOR ",") AS url, pcm.category_id
FROM product p
LEFT JOIN product_category pc ON pc.product_id = p.id
LEFT JOIN media m ON p.id = m.product_id
INNER JOIN product_category pcm ON p.id = pcm.product_id AND pcm.main = 1 AND pcm.product_id IS NOT NULL
LEFT JOIN product_filter_value pfv1 ON p.id = pfv1.product_id
LEFT JOIN product_filter_value pfv2 ON p.id = pfv2.product_id
LEFT JOIN product_filter_value pfv3 ON p.id = pfv3.product_id
LEFT JOIN product_filter_value pfv4 ON p.id = pfv4.product_id
WHERE (pc.category_id = 23)
AND (p.active = 1)
AND (pfv1.filter_value_id IN (2))
AND (pfv2.filter_value_id IN (8))
AND (pfv3.filter_value_id IN (22))
AND (pfv4.filter_value_id IN (38))
GROUP BY 1
ORDER BY p.top desc

dynamically joining the tables on queries slow performance

I want to show the data of more than 50 tables in one list view. I have formed the query below.
Left join is dynamically added using PHP forloop.
Any other ways to get values from various tables(more than 50 tables)?
Or please suggest any other technology to use PHP Product...
SELECT * FROM main_table `t`
LEFT JOIN `table` ON table.c_application_id = t.id
LEFT JOIN `table1` ON table1.c_application_id = t.id
LEFT JOIN `table2` ON table2.c_application_id = t.id
LEFT JOIN `table3` ON table3.c_application_id = t.id
LEFT JOIN `table4` ON table4.c_application_id = t.id
LEFT JOIN `table5` ON table5.c_application_id = t.id
LEFT JOIN `table6` ON table6.c_application_id = t.id
LEFT JOIN `table7` ON table7.c_application_id = t.id
LEFT JOIN `table8` ON table8.c_application_id = t.id
LEFT JOIN `table9` ON table9.c_application_id = t.id
LEFT JOIN `table10` ON table10.c_application_id = t.id
LEFT JOIN `table11` ON table11.c_application_id = t.id
LEFT JOIN `table12` ON table12.id=t.c_cycle
LEFT JOIN `table13` ON table13.c_groupId=t.c_program_to_apply
LEFT JOIN `table14` ON table14.id = table14.c_status_id
LEFT JOIN `table15` ON table15.id = table15.c_stage_id
LEFT JOIN `table16` ON table16.c_code = table16.c_country
LEFT JOIN `table17` ON table17.id=table17.c_app_form

how to use INNER JOIN and IN Clause in same query

i have three tables and want to run INNER JOIN and IN clause on them.
can anyone tell me where i am doing wrong
SELECT `tblinvoices`.id,`tblinvoices`.userid,`firstname`,`lastname`
FROM `tblinvoices`
WHERE `paymentmethod`IN
(SELECT `gateway` FROM `tblpaymentgateways` WHERE `setting`='type' AND `value` = 'CC')
INNER JOIN `tblclients` ON `tblinvoices`.userid=`tblclients`.id"
JOIN comes before WHERE:
SELECT tblinvoices.id,
tblinvoices.userid,
firstname,
lastname
FROM
tblinvoices
INNER JOIN tblclients
ON tblinvoices.userid = tblclients.id
WHERE
paymentmethod IN
(select gateway
FROM tblpaymentgateways
WHERE setting='type'
AND value = 'CC')

Left Join with subquery returns Null

I am applying Left Join with Sub query & where clause
It seems fine no syntax error but the columns I am selecting from that sub query always returns me Null. I have executed the same part in my SQL section, it gives me record. Kindly have a look on the query and let me know if any thing is possible or if question is not clear.
SELECT alt.userId, u.name, t.name AS teamName, alt.startDateTime, v.name AS villageName, c.name AS clusterName, startLat, startLong, latlng.lat, latlng.long
FROM activity_log_tim AS alt
JOIN user AS u ON u.userId = alt.userId
JOIN team_members AS tm ON tm.memberId = u.userId
JOIN team AS t ON t.teamId = tm.teamId
JOIN village AS v ON v.villageId = alt.villageId
JOIN cluster_villages AS cv ON cv.villageId = v.villageId
JOIN cluster AS c ON c.clusterId = cv.clusterId
LEFT JOIN (SELECT lat, long,dateTime, scheduleId FROM activity_log_gps LIMIT 1) AS latlng ON latlng.scheduleId = alt.scheduleId
WHERE DATE(alt.startDateTime) = '2015-09-05' AND DATE(alt.endDateTime) = '0000-00-00' GROUP BY alt.userId ORDER BY latlng.dateTime DESC
Well it's supposed to since you are performing a LEFT JOIN which produces NULL if no match occurs. So either you can perform a INNER JOIN or use a COALESCE function like below
SELECT alt.userId, u.name,
t.name AS teamName,
alt.startDateTime,
v.name AS
villageName,
c.name AS clusterName,
startLat,
startLong,
COALESCE(latlng.lat, 23), //Notice the use of COALESCE. If null will return 23 as default
COALESCE(latlng.long, 32)

Help with a joined query (MySQL)

Hello can anybody see why this query fails?
SELECT A.idAd, A.ads_in_Cat, A.title, A.currency, A.price,
A.in_dpt, A.description, A.featured FROM ads A
LEFT JOIN featured F ON F.ad = A.idAd
INNER JOIN dept D ON D.id_dept = A.in_dpt
INNER JOIN sub_cat_ad S ON S.id_sub_cat = A.ads_in_Cat
INNER JOIN cat_ad C ON C.idCat_ad = S.from_cat_ad
ORDER BY A.featured DESC LIMIT :limit, :offset
But this one works:
SELECT *FROM ads
LEFT JOIN featured ON featured.ad = ads.idAd
INNER JOIN dept ON dept.id_dept = ads.in_dpt
INNER JOIN sub_cat_ad ON id_sub_cat = ads.ads_in_Cat
INNER JOIN cat_ad ON idCat_ad = sub_cat_ad.from_cat_ad
ORDER BY featured DESC LIMIT :limit, :offset
In the first one, I don't want all columns from the table "ads", the query returns only columns till ...FROM ads A.
If you specify to only select fields from A that's what you get: Only fields from A.
If you want fields from other tables too you have to specify them as well.
SELECT
A.idAd, A.ads_in_Cat, A.title, A.currency, A.price, A.in_dpt, A.description, A.featured,
F.*,
D.*,
S.*,
C.*
FROM ads A
LEFT JOIN featured F ON F.ad = A.idAd
INNER JOIN dept D ON D.id_dept = A.in_dpt
INNER JOIN sub_cat_ad S ON S.id_sub_cat = A.ads_in_Cat
INNER JOIN cat_ad C ON C.idCat_ad = S.from_cat_ad
ORDER BY A.featured DESC LIMIT :limit, :offset

Categories