Here is big sql query and it works
select * from (SELECT DISTINCT `t`.*
FROM `wp_pods_bars` AS `t` LEFT JOIN `wp_podsrel` AS `rel_city`
ON ( `rel_city`.`field_id` = 13918 AND `rel_city`.`item_id` = `t`.`id` )
LEFT JOIN `wp_pods_city` AS `city`
ON ( `rel_city`.`field_id` = 13918 AND `city`.`id` = `rel_city`.`related_item_id` )
LEFT JOIN `wp_podsrel` AS `rel_goodfor_what`
ON ( `rel_goodfor_what`.`field_id` = 13912 AND `rel_goodfor_what`.`item_id` = `t`.`id` )
LEFT JOIN `wp_pods_goodfor` AS `goodfor_what`
ON ( `rel_goodfor_what`.`field_id` = 13912 AND `goodfor_what`.`id` = `rel_goodfor_what`.`related_item_id` )
LEFT JOIN `wp_podsrel` AS `rel_goodfor_who`
ON `rel_goodfor_who`.`field_id` = 13911 AND `rel_goodfor_who`.`item_id` = `t`.`id`
LEFT JOIN `wp_pods_goodfor_who` AS `goodfor_who`
ON `rel_goodfor_who`.`field_id` = 13911 AND `goodfor_who`.`id` = `rel_goodfor_who`.`related_item_id`
LEFT JOIN `wp_podsrel` AS `rel_goodfor_when`
ON `rel_goodfor_when`.`field_id` = 13913 AND `rel_goodfor_when`.`item_id` = `t`.`id`
LEFT JOIN `wp_pods_goodfor_when` AS `goodfor_when` ON `rel_goodfor_when`.`field_id` = 13913
AND `goodfor_when`.`id` = `rel_goodfor_when`.`related_item_id`
WHERE hide=0 AND opening_soon=0 AND ( `city`.`name` = "London" ) AND `goodfor_when`.`name` IN ('Late') GROUP BY name HAVING COUNT(DISTINCT `goodfor_when`.`name`) = 1
ORDER BY `t`.`name`, `t`.`id`
) as foo
LEFT JOIN `wp_podsrel` AS `pr`
ON `field_id` = 13905 AND `item_id` = foo.id
and returns 144 results. Now I add one more left join and no results (See in the bottom)
select * from (SELECT DISTINCT `t`.*
FROM `wp_pods_bars` AS `t` LEFT JOIN `wp_podsrel` AS `rel_city`
ON ( `rel_city`.`field_id` = 13918 AND `rel_city`.`item_id` = `t`.`id` )
LEFT JOIN `wp_pods_city` AS `city`
ON ( `rel_city`.`field_id` = 13918 AND `city`.`id` = `rel_city`.`related_item_id` )
LEFT JOIN `wp_podsrel` AS `rel_goodfor_what`
ON ( `rel_goodfor_what`.`field_id` = 13912 AND `rel_goodfor_what`.`item_id` = `t`.`id` )
LEFT JOIN `wp_pods_goodfor` AS `goodfor_what`
ON ( `rel_goodfor_what`.`field_id` = 13912 AND `goodfor_what`.`id` = `rel_goodfor_what`.`related_item_id` )
LEFT JOIN `wp_podsrel` AS `rel_goodfor_who`
ON `rel_goodfor_who`.`field_id` = 13911 AND `rel_goodfor_who`.`item_id` = `t`.`id`
LEFT JOIN `wp_pods_goodfor_who` AS `goodfor_who`
ON `rel_goodfor_who`.`field_id` = 13911 AND `goodfor_who`.`id` = `rel_goodfor_who`.`related_item_id`
LEFT JOIN `wp_podsrel` AS `rel_goodfor_when`
ON `rel_goodfor_when`.`field_id` = 13913 AND `rel_goodfor_when`.`item_id` = `t`.`id`
LEFT JOIN `wp_pods_goodfor_when` AS `goodfor_when` ON `rel_goodfor_when`.`field_id` = 13913
AND `goodfor_when`.`id` = `rel_goodfor_when`.`related_item_id`
WHERE hide=0 AND opening_soon=0 AND ( `city`.`name` = "London" ) AND `goodfor_when`.`name` IN ('Late') GROUP BY name HAVING COUNT(DISTINCT `goodfor_when`.`name`) = 1
ORDER BY `t`.`name`, `t`.`id`
) as foo
LEFT JOIN `wp_podsrel` AS `pr`
ON `field_id` = 13905 AND `item_id` = foo.id
LEFT JOIN `wp_posts`
ON `pr`.related_item_id = `wp_posts`.`ID`
So now this query returns 0 results. I suppose it should add just empty rows if no match found? Here is wp_podsrel structure
`wp_podsrel` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`pod_id` int(10) unsigned DEFAULT NULL,
`field_id` int(10) unsigned DEFAULT NULL,
`item_id` bigint(20) unsigned DEFAULT NULL,
`related_pod_id` int(10) unsigned DEFAULT NULL,
`related_field_id` int(10) unsigned DEFAULT NULL,
`related_item_id` bigint(20) unsigned DEFAULT NULL,
`weight` smallint(5) unsigned DEFAULT '0',
PRIMARY KEY (`id`),
KEY `field_item_idx` (`field_id`,`item_id`),
KEY `rel_field_rel_item_idx` (`related_field_id`,`related_item_id`),
KEY `field_rel_item_idx` (`field_id`,`related_item_id`),
KEY `rel_field_item_idx` (`related_field_id`,`item_id`)
)
Related
I have some troubles to finish my Mysql Query to return me what I need to have. I am new to such long queries in MYSQL.
SELECT
lang_rel_a_id,
lang_rel_b_id,
lang_rel_id,
tla.text_lang_t AS atext,
lald.lang_data_lang_id AS laid,
lald.lang_data_position AS lapp,
lald.lang_data_font_weight AS lafw,
lald.lang_data_font_size AS lafs,
lald.lang_data_font_color AS lafc,
lald.lang_data_bg_color AS labg,
lasdf.funca AS lafunc,
lang_ship,
lbld.lang_data_lang_id AS lbid,
lbld.lang_data_position AS lbpp,
lbld.lang_data_font_weight AS lbfw,
lbld.lang_data_font_size AS lbfs,
lbld.lang_data_font_color AS lbfc,
lbld.lang_data_bg_color AS lbbg,
tlb.text_lang_t AS btext,
lbsdf.funcb AS lbfunc
FROM lang_relation
LEFT JOIN
(SELECT *, GROUP_CONCAT(text_func_t SEPARATOR ', ') AS funca
FROM synt_data_func
LEFT JOIN text_func ON text_func_id = synt_df_func
GROUP BY synt_df_lang_data
)
lasdf ON lang_rel_a_id = lasdf.synt_df_lang_data
LEFT JOIN lang_data lald ON lald.lang_data_id = lang_rel_a_id
LEFT JOIN text_lang tla ON lald.lang_data_lang_id = tla.text_lang_id
LEFT JOIN
(SELECT *, GROUP_CONCAT(text_func_t SEPARATOR ', ') AS funcb
FROM synt_data_func
LEFT JOIN text_func ON text_func_id = synt_df_func
GROUP BY synt_df_lang_data
)
lbsdf ON lang_rel_b_id = lbsdf.synt_df_lang_data
LEFT JOIN lang_data lbld ON lbld.lang_data_id = lang_rel_b_id
LEFT JOIN text_lang tlb ON lbld.lang_data_lang_id = tlb.text_lang_id
WHERE lang_rel_a_id < lang_rel_b_id
GROUP BY lang_rel_id
I have a relation of two languages in my lang_relation table. I need to query for each of them 2 subtables but the one of them is a relation table that contains the relation between the lang_data_id (= lang_rel_a_id OR lang_rel_b_id, = synt_df_lang_data) and text of the different language functions where multiple values are possible.
I do not understand why the group_concat in this subquery returns only one row. If I do only this query, I get all the results. But when I put it into this larger query, everything's fine but this.. is ..not.
My language_relation table
CREATE TABLE `lang_relation`
(
`lang_rel_id` int(11) NOT NULL,
`lang_rel_a_id` int(11) NOT NULL,
`lang_rel_b_id` int(11) NOT NULL,
`lang_ship` tinyint(1) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
The joined lang_data
CREATE TABLE `lang_data` (
`lang_data_id` int(11) NOT NULL,
`lang_data_pic_key` int(11) NOT NULL,
`lang_data_position` tinyint(1) NOT NULL,
`lang_data_lang_id` int(11) NOT NULL,
`lang_data_font_weight` tinyint(2) NOT NULL,
`lang_data_font_size` tinyint(2) NOT NULL,
`lang_data_font_color` tinyint(2) NOT NULL,
`lang_data_bg_color` tinyint(2) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
And the synt_data_func. The text_table is a simple 2-column-table with id + text.
CREATE TABLE `synt_data_func` (
`synt_df_id` int(11) NOT NULL,
`synt_df_lang_data` int(11) NOT NULL,
`synt_df_func` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
I tried different approachs. This seems to be the one that is the nearest of what I need. I don't know how many times I changed the GROUP BY clauses, I even tried to do the CONCAT_GROUP in the parent SELECT..
I even wonder if this is possible because the subqueries are going for 2 different IDs .. is this the problem?
Thanks for any hint in advance.
I finally got it. Maybe it will help somebody with a similar question. I changed my approach for this query.
SELECT
lrel.lang_rel_pic_key,
lrel.lang_rel_id,
langdata_a.lascore,
lasdf.func_a,
langdata_a.latext,
lasf.score_astyle,
SUM(lasf.score_astyle) + (langdata_a.lascore) AS atotal,
lang_ship,
langdata_b.lbtext,
langdata_b.lbscore,
lbsdf.func_b,
lbsf.bformat,
lbsf.score_bstyle,
SUM(lbsf.score_bstyle) + (langdata_b.lbscore) AS btotal
FROM lang_relation lrel
INNER JOIN
(
SELECT DISTINCT
lald.lang_data_id,
lafw.field_value AS lafweight,
lafs.field_value AS lafsize,
lafc.field_value AS laffc,
lafbg.field_value AS lafbg,
lapos.field_value AS laposa,
tla.text_lang_t AS latext,
SUM(lafw.field_value) + (lafs.field_value) + (lafc.field_value) + (lafbg.field_value) + (lapos.field_value) AS lascore
FROM lang_data lald
LEFT JOIN text_lang tla ON lald.lang_data_lang_id = tla.text_lang_id
LEFT JOIN `fields` lafw ON lald.lang_data_font_weight = lafw.field_id
LEFT JOIN `fields` lafs ON lald.lang_data_font_size = lafs.field_id
LEFT JOIN `fields` lafc ON lald.lang_data_font_color = lafc.field_id
LEFT JOIN `fields` lafbg ON lald.lang_data_bg_color = lafbg.field_id
LEFT JOIN `fields` lapos ON lald.lang_data_position = lapos.field_id
GROUP BY lald.lang_data_id
)
langdata_a ON langdata_a.lang_data_id = lrel.lang_rel_a_id
LEFT JOIN
(SELECT sdf.synt_df_lang_data, GROUP_CONCAT(latf.text_func_t) AS func_a
FROM synt_data_func sdf
INNER JOIN text_func latf ON latf.text_func_id = sdf.synt_df_func
GROUP BY sdf.synt_df_lang_data
)
lasdf ON lasdf.synt_df_lang_data = lrel.lang_rel_a_id
LEFT JOIN
(
SELECT sfb.synt_format_lang_data,
sfb.synt_format_fields_id,
GROUP_CONCAT(sfbf.field_text SEPARATOR ', ') AS aformat,
SUM(sfbf.field_value) AS score_astyle
FROM synt_format sfb
INNER JOIN `fields` sfbf ON sfbf.field_id = sfb.synt_format_fields_id
GROUP BY sfb.synt_format_lang_data
)
lasf ON lasf.synt_format_lang_data = lrel.lang_rel_a_id
INNER JOIN
(
SELECT DISTINCT
lbld.lang_data_id,
lbfw.field_value AS lbfweight,
lbfs.field_value AS lbfsize,
lbfc.field_value AS lbffc,
lbfbg.field_value AS lbfbg,
lbpos.field_value AS lbposa,
tlb.text_lang_t AS lbtext,
SUM(lbfw.field_value) + (lbfs.field_value) + (lbfc.field_value) + (lbfbg.field_value) + (lbpos.field_value) AS lbscore
FROM lang_data lbld
LEFT JOIN text_lang tlb ON lbld.lang_data_lang_id = tlb.text_lang_id
LEFT JOIN `fields` lbfw ON lbld.lang_data_font_weight = lbfw.field_id
LEFT JOIN `fields` lbfs ON lbld.lang_data_font_size = lbfs.field_id
LEFT JOIN `fields` lbfc ON lbld.lang_data_font_color = lbfc.field_id
LEFT JOIN `fields` lbfbg ON lbld.lang_data_bg_color = lbfbg.field_id
LEFT JOIN `fields` lbpos ON lbld.lang_data_position = lbpos.field_id
GROUP BY lbld.lang_data_id
)
langdata_b ON langdata_b.lang_data_id = lrel.lang_rel_b_id
LEFT JOIN
(SELECT sdfb.synt_df_lang_data, GROUP_CONCAT(lbtf.text_func_t) AS func_b
FROM synt_data_func sdfb
INNER JOIN text_func lbtf ON lbtf.text_func_id = sdfb.synt_df_func
GROUP BY sdfb.synt_df_lang_data
)
lbsdf ON lbsdf.synt_df_lang_data = lrel.lang_rel_b_id
LEFT JOIN
(
SELECT sfb.synt_format_lang_data,
sfb.synt_format_fields_id,
GROUP_CONCAT(sfbf.field_text SEPARATOR ', ') AS bformat,
SUM(sfbf.field_value) AS score_bstyle
FROM synt_format sfb
INNER JOIN `fields` sfbf ON sfbf.field_id = sfb.synt_format_fields_id
GROUP BY sfb.synt_format_lang_data
)
lbsf ON lbsf.synt_format_lang_data = lrel.lang_rel_b_id
GROUP BY lrel.lang_rel_id
Maybe a bit long, but output is exactly what was needed :-)
I am trying to get the total of my intax and outTax. I have the right query, if i run it shows proper result. But i am not able to display it as i need.
Here is my code.
$sqlOut = "SELECT sales_invoice.invoice_id, MONTHNAME(sales_invoice.date_invoiced) AS month, sales_invoice_line_items.invoice_id, sales_invoice_line_items.tax, SUM(sales_invoice_line_items.tax_amount) AS totaltax, taxes.tax_id, taxes.rate, taxes.name AS Tname FROM sales_invoice INNER JOIN sales_invoice_line_items ON sales_invoice.invoice_id=sales_invoice_line_items.invoice_id INNER JOIN taxes ON sales_invoice_line_items.tax=taxes.tax_id WHERE sales_invoice_line_items.tax=".$tax." GROUP BY sales_invoice.date_invoiced";
$sqlIn = "SELECT purchase_invoice.invoice_id, MONTHNAME(purchase_invoice.date_invoiced) AS month, purchase_invoice_line_items.invoice_id, purchase_invoice_line_items.tax, SUM(purchase_invoice_line_items.tax_amount) AS totaltax, taxes.tax_id, taxes.rate, taxes.name AS Tname FROM purchase_invoice INNER JOIN purchase_invoice_line_items ON purchase_invoice.invoice_id=purchase_invoice_line_items.invoice_id INNER JOIN taxes ON purchase_invoice_line_items.tax=taxes.tax_id WHERE purchase_invoice_line_items.tax=".$tax." GROUP BY purchase_invoice.date_invoiced";
$ResOut = mysql_query($sqlOut) or die(mysql_error());
$ResIn = mysql_query($sqlIn) or die(mysql_error());
}
I want to display it like this
<td>Months</td><td>Out Tax</td><td>In Tax</td><td>Difference(OutTax-InTax)</td>
My output format has to be
<table><tr>
<td>Months</td><td>Out Tax</td><td>In Tax</td><td>Difference(OutTax-InTax)</td></tr>
<tr><td>Jan</td> <td>3456</td> <td>2311</td> <td>1145</td></tr>
<tr><td>March</td> <td>4123</td> <td>3125</td> <td>2978</td></tr>
</table>
And table structure is
purchase_invoice table
CREATE TABLE IF NOT EXISTS `purchase_invoice` (
`invoice_id` int(50) NOT NULL AUTO_INCREMENT,
`order_id` int(50) NOT NULL,
`date_invoiced` date NOT NULL,
`status` varchar(10) NOT NULL,
PRIMARY KEY (`invoice_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ;
purchase_invoice_line_items
CREATE TABLE IF NOT EXISTS `purchase_invoice_line_items` (
`id` int(50) NOT NULL AUTO_INCREMENT,
`invoice_id` int(50) NOT NULL,
`tax` int(10) NOT NULL,
`discount` int(10) NOT NULL,
`freight` int(20) NOT NULL,
`sub_total` double NOT NULL,
`tax_amount` double NOT NULL,
`reason` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=8 ;
Can somebody please help me in this
Try this :
SELECT A.month_invoiced AS 'Months', A.totaltax AS 'Out Tax',
B.totaltax AS 'In Tax', (A.totaltax - B.totaltax) AS 'Difference(OutTax-InTax)'
FROM (SELECT MONTHNAME(si.date_invoiced) AS month_invoiced,
EXTRACT(YEAR_MONTH FROM si.date_invoiced) AS YM_Invoced,
SUM(sil.tax_amount) AS totaltax
FROM sales_invoice si
INNER JOIN sales_invoice_line_items sil ON si.invoice_id=sil.invoice_id
INNER JOIN taxes t ON sil.tax=t.tax_id
WHERE sil.tax=".$tax."
GROUP BY YM_Invoced
) AS A
INNER JOIN (SELECT MONTHNAME(pi.date_invoiced) AS month_invoiced,
EXTRACT(YEAR_MONTH FROM pi.date_invoiced) AS YM_Invoced,
SUM(pil.tax_amount) AS totaltax
FROM purchase_invoice PI
INNER JOIN purchase_invoice_line_items pil ON pi.invoice_id=pil.invoice_id
INNER JOIN taxes t ON pil.tax=t.tax_id
WHERE pil.tax=".$tax."
GROUP BY YM_Invoced
) AS B ON A.YM_Invoced = B.YM_Invoced;
::EDIT::
SELECT A.month_invoiced,
MAX(CASE WHEN A.taxType = 'Out' THEN A.totaltax ELSE 0 END) AS 'Out Tax',
MAX(CASE WHEN A.taxType = 'In' THEN A.totaltax ELSE 0 END) AS 'In Tax',
(IFNULL(MAX(CASE WHEN A.taxType = 'Out' THEN A.totaltax ELSE 0 END), 0) -
IFNULL(MAX(CASE WHEN A.taxType = 'In' THEN A.totaltax ELSE 0 END), 0)
) AS 'Difference(OutTax-InTax)'
FROM (SELECT MONTHNAME(si.date_invoiced) AS month_invoiced,
EXTRACT(YEAR_MONTH FROM si.date_invoiced) AS YM_Invoced,
SUM(sil.tax_amount) AS totaltax,
'Out' AS taxType
FROM sales_invoice si
INNER JOIN sales_invoice_line_items sil ON si.invoice_id=sil.invoice_id
INNER JOIN taxes t ON sil.tax=t.tax_id
WHERE sil.tax=".$tax."
GROUP BY YM_Invoced
UNION
SELECT MONTHNAME(pi.date_invoiced) AS month_invoiced,
EXTRACT(YEAR_MONTH FROM pi.date_invoiced) AS YM_Invoced,
SUM(pil.tax_amount) AS totaltax,
'IN' AS taxType
FROM purchase_invoice PI
INNER JOIN purchase_invoice_line_items pil ON pi.invoice_id=pil.invoice_id
INNER JOIN taxes t ON pil.tax=t.tax_id
WHERE pil.tax=".$tax."
GROUP BY YM_Invoced
) AS A
GROUP BY A.YM_Invoced
I'm getting the error message '#1111 - Invalid use of group function' with the following query and I'm not entirely sure why.
I originally had each of the three components of the SUM as individual views (you can see the remnants of that setup in the JOIN list), but it was taking too long to run that view due to not being able to use indexes in joining view.
SELECT
`VehicleClaim`.`id` AS `Vehicle_claim_id`, `VehicleClaim`.`Vehicle_registration_number`,
`Claim`.`id` AS `claim_id`, `Claim`.`created` AS `claim_created`, DATE_FORMAT(`Claim`.`created`, '%b') AS `claim_created_month`,
`ClaimSubtype`.`name` AS `claim_subtype`,
`Vendor`.`name` as `vendor_name`,
IF(`VehicleClaim`.`id` IS NOT NULL,
IF(`VehicleClaimUpdate`.`id` IS NOT NULL, `VehicleClaimUpdateStatus`.`status`, `ClaimStatus`.`status`),
IF(`ClaimUpdate`.`id` IS NOT NULL, `ClaimUpdateStatus`.`status`, `ClaimStatus`.`status`)
) AS `status`,
IF(`VehicleClaimUpdate`.`id` IS NOT NULL, `VehicleClaimUpdate`.`result`,
IF(`ClaimUpdate`.`id` IS NOT NULL, `ClaimUpdate`.`result`, NULL)
) AS `result`,
SUM(
(IF((`VehicleClaimUpdate`.`conceded_labour_cost` is not null),
`VehicleClaimUpdate`.`conceded_labour_cost`,
IF((`ClaimUpdate`.`id` is not null),
`ClaimUpdate`.`conceded_labour_cost`,
IF((`Claim`.`repair_order_id` is not null),
IF((`RepairOrder`.`cost` is not null),
`RepairOrder`.`cost`,
SUM((`RepairOrder`.`removal_refit_hours` * `RepairOrderHourlyRate`.`rate`))
),
IF((`VehicleClaim`.`id` is not null),SUM((`VehicleClaim`.`work_hours` * `VehicleClaimHourlyRate`.`rate`)),0)
)
)
))
+
(IF((`VehicleClaimUpdate`.`conceded_material_cost` is not null),
`VehicleClaimUpdate`.`conceded_material_cost`,
IF((`ClaimUpdate`.`id` is not null),
`ClaimUpdate`.`conceded_material_cost`,
IF((`ClaimMaterial`.`id` is not null),SUM((((`ClaimMaterial`.`unit_cost` * `ClaimMaterial`.`quantity`) * `ClaimMaterial`.`percentage_claimed`) / 100)),0)
)
))
+
(IF(`VehicleClaimUpdate`.`conceded_freight_cost` IS NOT NULL,
`VehicleClaimUpdate`.`conceded_freight_cost`,
IF(`ClaimUpdate`.`id` IS NOT NULL,
`ClaimUpdate`.`conceded_freight_cost`,
IF(`ClaimFreight`.`id` IS NOT NULL, `ClaimFreight`.`cost`, 0)
)
))
) AS `total_cost`,
`User`.`id` AS `user_id`, `User`.`username`,
`Client`.`id` AS `client_id`
FROM `Vehicle_claims` AS `VehicleClaim`
RIGHT JOIN `claims` AS `Claim` ON (`Claim`.`id` = `VehicleClaim`.`claim_id`)
LEFT JOIN `claim_materials` AS `ClaimMaterial` ON (`ClaimMaterial`.`claim_id` = `Claim`.`id`)
LEFT JOIN `claim_freight` AS `ClaimFreight` ON (`ClaimFreight`.`claim_id` = `Claim`.`id`)
LEFT JOIN `claim_subtypes` AS `ClaimSubtype` ON (`ClaimSubtype`.`id` = `Claim`.`claim_subtype_id`)
LEFT JOIN `repair_orders` AS `RepairOrder` ON (`RepairOrder`.`id` = `Claim`.`repair_order_id`)
LEFT JOIN `hourly_rates` AS `RepairOrderHourlyRate` ON (`RepairOrderHourlyRate`.`id` = `RepairOrder`.`hourly_rate_id`)
LEFT JOIN `hourly_rates` AS `VehicleClaimHourlyRate` ON (`VehicleClaimHourlyRate`.`id` = `VehicleClaim`.`hourly_rate_id`)
-- LEFT JOIN `view_final_material_cost` AS `FinalMaterialCost` ON (`FinalMaterialCost`.`claim_id` = `Claim`.`id` AND `FinalMaterialCost`.`Vehicle_claim_id` = `VehicleClaim`.`id`)
-- LEFT JOIN `view_final_freight_cost` AS `FinalFreightCost` ON (`FinalFreightCost`.`claim_id` = `Claim`.`id` AND `FinalFreightCost`.`Vehicle_claim_id` = `VehicleClaim`.`id`)
-- LEFT JOIN `view_final_labour_cost` AS `FinalLabourCost` ON (`FinalLabourCost`.`claim_id` = `Claim`.`id` AND `FinalLabourCost`.`Vehicle_claim_id` = `VehicleClaim`.`id`)
LEFT JOIN `vendor_contacts` AS `VendorContact` ON (`VendorContact`.`id` = `Claim`.`vendor_contact_id`)
LEFT JOIN `vendors` AS `Vendor` ON (`Vendor`.`id` = `VendorContact`.`vendor_id`)
LEFT JOIN `users` AS `User` ON (`User`.`id` = `Claim`.`user_id`)
LEFT JOIN `clients` AS `Client` ON (`Client`.`id` = `Claim`.`client_id`)
LEFT JOIN `claim_statuses` AS `ClaimStatus` ON (`ClaimStatus`.`id` = `Claim`.`claim_status_id`)
LEFT JOIN `claim_updates` AS `ClaimUpdate` ON (`ClaimUpdate`.`id` = `Claim`.`claim_update_id`)
left join `claim_statuses` AS `ClaimUpdateStatus` on (`ClaimUpdateStatus`.`id` = `ClaimUpdate`.`claim_status_id`)
left join `claim_statuses` AS `VehicleClaimStatus` on (`ClaimStatus`.`id` = `VehicleClaim`.`claim_status_id`)
LEFT JOIN `claim_updates` AS `VehicleClaimUpdate` ON (`VehicleClaimUpdate`.`id` = `VehicleClaim`.`claim_update_id`)
left join `claim_statuses` AS `VehicleClaimUpdateStatus` on (`VehicleClaimUpdateStatus`.`id` = `VehicleClaimUpdate`.`claim_status_id`)
WHERE YEAR(`Claim`.`created`) = YEAR(CURDATE())
GROUP BY `VehicleClaim`.`id`
Hi i have trouble with this query
SELECT * FROM(
SELECT `b`.*,`owner`.firstname,`owner`.lastname,`owner`.email,
(
SELECT COUNT(`ps`.profile_id) FROM `profile` AS `ps`
LEFT JOIN `xref_store_profile_brand` AS `xbp` ON `xbp`.profile_id = `ps`.profile_id
WHERE `xbp`.brand_id = b.brand_id AND ps.role = 'salesrep' AND `xbp`.store_id IS NULL
) AS `salesrepTotal`,
(
SELECT GROUP_CONCAT(`ms`.firstname) FROM `profile` AS `ps`
LEFT JOIN `xref_store_profile_brand` AS `xbp` ON `xbp`.profile_id = `ps`.profile_id
LEFT JOIN `member` AS `ms`ON `ms`.member_id = `ps`.member_id
WHERE `xbp`.brand_id = `b`.brand_id AND ps.role = 'salesrep' AND `xbp`.store_id IS NULL
) AS `salesrep`,
(
SELECT COUNT(`s`.store_id) FROM `store` AS `s`
LEFT JOIN `xref_store_profile_brand` AS `xbs` ON `xbs`.store_id = `s`.store_id
WHERE `xbs`.brand_id = `b`.brand_id AND `xbs`.brand_id IS NOT NULL
) AS `storeTotal`,
(
SELECT GROUP_CONCAT(`s`.name) FROM `store` AS `s`
LEFT JOIN `xref_store_profile_brand` AS `xbs` ON `xbs`.store_id = `s`.store_id
WHERE `xbs`.brand_id = `b`.brand_id AND `xbs`.brand_id IS NOT NULL
) AS `store`
FROM `brand` AS `b`
LEFT JOIN
(
SELECT `m`.firstname,`m`.lastname,`m`.email,`xspb`.brand_id FROM `member` AS `m`
LEFT JOIN `profile` as `p` ON `p`.member_id = `m`.member_id AND `p`.role = 'designer' AND `p`.isPrimary = 1
LEFT JOIN `xref_store_profile_brand` AS `xspb` ON `xspb`.profile_id = `p`.profile_id AND `xspb`.store_id IS NULL
) AS `owner` ON `owner`.brand_id =`b`.brand_id
GROUP BY `b`.brand_id
) AS `final`
how can i convert this in to Zend_Db_Select object?
Th main problem is
this part
SELECT `b`.*,`owner`.firstname,`owner`.lastname,`owner`.email,
(
SELECT COUNT(`ps`.profile_id) FROM `profile` AS `ps`
LEFT JOIN `xref_store_profile_brand` AS `xbp` ON `xbp`.profile_id = `ps`.profile_id
WHERE `xbp`.brand_id = b.brand_id AND ps.role = 'salesrep' AND `xbp`.store_id IS NULL
) AS `salesrepTotal`,
You need to use Zend_Db_Expr objects in your query and array structures for select AS.
below is the solution you are looking for:
<?php
$db = Zend_Db_Table::getDefaultAdapter();
// inner query
$sqlSalesRepTotal = $db->select()
->from(array('ps' => 'profile'))
->joinLeft(array('xbp' => 'xref_store_profile_brand'), 'xbp.profile_id = ps.profile_id')
->where('xbp.brand_id = b.brand_id')
->where('ps.role = ?', 'salesrep')
->where('xbp.store_id IS NULL');
// main query
$sql = $db->select()
->from(array('b' => 'brand'), array(
// NOTE: have to add parentesis around the expression
'salesrepTotal' => new Zend_Db_Expr("($sqlSalesRepTotal)")
))
->where('....')
->group('brand_id');
// debug
var_dump($db->fetchAll($sql));
I have a code like this:
SELECT
`cabinet`.`name_test`,
`answers`.`answer_id`,
(
SELECT `answer_id`
FROM `answers`
WHERE `user_id` = '353281' AND `answers`.`test_id` = `cabinet`.`test_id`
) as answer_2
FROM `cabinet`
INNER JOIN `answers`
WHERE
`user_id` = '184918649'
AND `friend_id` = '353281'
AND `answers`.`test_id` = `cabinet`.`test_id`
How not to write twice: answers.test_id = cabinet.test_id?
It is not really less code, but you can also do this by joining on the answers table twice:
SELECT
c.`name_test`,
a1.`answer_id` answer_1,
a2.`answer_id` answer_2
FROM `cabinet` c
INNER JOIN `answers` a1
ON c.`test_id` = a1.`test_id`
AND a1.`user_id` = '184918649'
AND `friend_id` = '353281'
INNER JOIN `answers` a2
ON c.`test_id` = a2.`test_id`
AND a2.`user_id` = '353281'
Or you can use subqueries:
SELECT
c.`name_test`,
a1.`answer_id` answer_1,
a2.`answer_id` answer_2
FROM `cabinet` c
INNER JOIN
(
select `answer_id`
from `answers`
where `user_id` = '184918649'
AND `friend_id` = '353281'
) a1
ON c.`test_id` = a1.`test_id`
INNER JOIN
(
select `answer_id`
from `answers`
where `user_id` = '353281'
) a2
ON c.`test_id` = a2.`test_id`;
you are missing ON clause in your sql
try this
SELECT
`cabinet`.`name_test`,
`answers`.`answer_id`,
(
SELECT `answer_id`
FROM `answers`
WHERE `user_id` = '353281' AND `answers`.`test_id` = `cabinet`.`test_id`
) as answer_2
FROM `cabinet`
INNER JOIN `answers` on `answers`.`test_id` = `cabinet`.`test_id`
WHERE
`user_id` = '184918649'
AND `friend_id` = '353281'
removed the last line which is double