$query = IroningOrder::find()->alias("io")->select(["io.*", "u1.username as createdByUser", "u2.username as updatedByUser", "concat(c.first_name, ' ', c.last_name) AS customer_name", "CONCAT(ud1.first_name , ' ', ud1.last_name) as pickedUpBy", "CONCAT(ud2.first_name , ' ', ud2.last_name) as deliveredBy", "ioe3.updated_at as delivered_at"])
->join("LEFT JOIN", "user u1", "u1.id = io.created_by")
->join("LEFT JOIN", "user u2", "u2.id = io.updated_by")
->join("INNER JOIN", "tbl_customer c", "c.id = io.customer_id")
->join("LEFT JOIN", "tbl_ironing_order_emp_allocation ioe1", "ioe1.ironing_order_id = io.id")
->join("LEFT JOIN", "tbl_ironing_order_emp_allocation ioe2", "ioe2.ironing_order_id = io.id")
->join("LEFT JOIN", "tbl_ironing_order_emp_allocation ioe3", "ioe3.ironing_order_id = io.id AND ioe3.type = 'DELIVERY' AND io.delivery_status = 'COMPLETE'")
->join("LEFT JOIN", "tbl_user_details ud1", "ud1.user_id = ioe1.user_id AND ioe1.type = 'PICKUP'")
->join("LEFT JOIN", "tbl_user_details ud2", "ud2.user_id = ioe2.user_id AND ioe2.type = 'DELIVERY'")
->join("LEFT JOIN", "tbl_delivery_address da", "da.id = io.delivery_address_id")
->groupBy(["io.id"]);
SELECT `io`.`id`, `io`.`customer_id`, `io`.`status`, `io`.`pickup_status`, `io`.`allocated_bag_id`, `io`.`inward_status`, `io`.`outward_status`, `io`.`ironman_status`, `io`.`created_at`, `io`.`updated_at`, `io`.`delivery_status`, `u1`.`username` AS `createdByUser`, `io`.`total`, `u2`.`username` AS `updatedByUser`, concat(c.first_name, ' ', c.last_name) AS customer_name, CONCAT(ud1.first_name , ' ', ud1.last_name) as pickedUpBy, CONCAT(ud2.first_name , ' ', ud2.last_name) as deliveredBy, `ioe3`.`updated_at` AS `delivered_at`
FROM `tbl_ironing_order` `io`
LEFT JOIN `user` `u1` ON u1.id = io.created_by
LEFT JOIN `user` `u2` ON u2.id = io.updated_by
INNER JOIN `tbl_customer` `c` ON c.id = io.customer_id
LEFT JOIN `tbl_ironing_order_emp_allocation` `ioe1` ON ioe1.ironing_order_id = io.id
LEFT JOIN `tbl_ironing_order_emp_allocation` `ioe2` ON ioe2.ironing_order_id = io.id
LEFT JOIN `tbl_ironing_order_emp_allocation` `ioe3` ON ioe3.ironing_order_id = io.id AND ioe3.type = 'DELIVERY' AND io.delivery_status = 'COMPLETE' LEFT JOIN `tbl_user_details` `ud1` ON ud1.user_id = ioe1.user_id AND ioe1.type = 'PICKUP'
LEFT JOIN `tbl_user_details` `ud2` ON ud2.user_id = ioe2.user_id AND ioe2.type = 'DELIVERY'
LEFT JOIN `tbl_delivery_address` `da` ON da.id = io.delivery_address_id GROUP BY `io`.`id`
When i run this query it will give me proper result set,But it will take too much time to load page.Please help me to optimize above query
Related
Query
(SELECT
pid,
visitdate,
GROUP_CONCAT(tooth, ' - ', problem, ' - ', recomendation SEPARATOR ', <br>')
FROM tbl_finds_d) V
WHERE V.pid='1' AND V.visitdate = '16-03-2020'
TABLE
Want to get WHERE pid=1 AND visitdate=16-03-2020
pid visitdate tooth problem recomendation
1 16-03-2020 13 ASX DFFF
1 16-03-2020 12 JHJ HJLP
2 12-03-2020 14 JKB IJLHJ
UPDATE (copied from the comment)
SELECT *
FROM pendingtreatment A
INNER JOIN tbl_finds_m B ON B.pid = '$pid'
AND B.visitdate = '$visitdate'
INNER JOIN treatmentadviced C ON C.pid = '$pid'
AND C.visitdate = '$visitdate'
INNER JOIN treatmentlist D ON D.pid = '$pid'
AND D.visitdate = '$visitdate'
INNER JOIN tbl_appointments E ON E.pid = '$pid'
AND E.visitdate = '$visitdate'
INNER JOIN ( SELECT pid,
visitdate,
GROUP_CONCAT(tooth, ' - ', problem, ' - ', recomendation SEPARATOR ', <br>')
FROM tbl_finds_d V
WHERE V.pid='$pid'
AND V.visitdate = '$visitdate') F
WHERE A.pid = '$pid'
AND A.visitdate = '$visitdate'
I'm currently stuck here and don't know what to do next since I can't use the tv_main alias into the 2 level deep subquery. Here's my code (I commented the part that have a problem). Any help will be appreciated. Thanks.
SELECT tv_main.id,
tv_main.vesselName,
(
SELECT SUM(t_statCtr.status = 'EX CREW')
FROM
(
(
SELECT tpi_stat.id,
tvv.vesselName,
lastname,
firstname,
middlename,
IF(tpi_stat.returningCrew = 1, 'NEW HIRE',
IF(COUNT(tc_ctr.personnel_id) > 1, 'EX CREW', 'NEW HIRE')
)
AS status
FROM tbl_contracts AS tc_stat
LEFT JOIN tbl_personnel_info AS tpi_stat
ON tpi_stat.id = tc_stat.personnel_id
LEFT JOIN tbl_contracts AS tc_ctr
ON tpi_stat.id = tc_ctr.personnel_id
LEFT JOIN tbl_vessels AS tvv
ON tvv.id = tpi_stat.lastJoinedVsl
WHERE
tpi_stat.emp_status = 'ON-BOARD'
AND tc_stat.status = 'ACTIVE'
AND tvv.id = tv_main.id --This line have an error, (Unknown Column tv_main.id in where clause)
GROUP BY tc_stat.personnel_id
) AS t_statCtr
)
) AS ex_crew,
NULL AS new_hire
FROM tbl_vessels AS tv_main -- I need this one to use inside the subquery
LEFT JOIN tbl_personnel_info AS tpi
ON tv_main.id = tpi.lastJoinedVsl
LEFT JOIN tbl_contracts AS tc
ON tpi.id = tc.personnel_id
WHERE
tpi_stat.emp_status = 'ON-BOARD'
AND tc_stat.status = 'ACTIVE'
GROUP BY tv_main.vesselName
I finally solve it. I didn't know that mysql only allow correlation of 1 deep level.
SELECT tv_main.vesselName,
SUM(t_dummy.statusx = 'EX CREW') AS ex_crew,
SUM(t_dummy.statusx = 'NEW HIRE') AS new_hire
FROM tbl_vessels AS tv_main
LEFT JOIN tbl_personnel_info AS tpi_main
ON tpi_main.lastJoinedVsl = tv_main.id
LEFT JOIN tbl_contracts AS tc_main
ON tc_main.personnel_id = tpi_main.id
LEFT JOIN
(
SELECT tvv.id,
tpi_stat.id AS tpiid,
IF(tpi_stat.returningCrew = 1, 'NEW HIRE',
IF(COUNT(tc_ctr.personnel_id) > 1, 'EX CREW', 'NEW HIRE')
)
AS statusx
FROM tbl_contracts AS tc_stat
LEFT JOIN tbl_personnel_info AS tpi_stat
ON tpi_stat.id = tc_stat.personnel_id
LEFT JOIN tbl_contracts AS tc_ctr
ON tpi_stat.id = tc_ctr.personnel_id
LEFT JOIN tbl_vessels AS tvv
ON tvv.id = tpi_stat.lastJoinedVsl
GROUP BY tc_stat.personnel_id
) AS t_dummy
ON tpi_main.id = t_dummy.tpiid
WHERE
tpi_main.emp_status = 'ON-BOARD'
AND tc_main.status = 'ACTIVE'
AND t_dummy.id = tv_main.id
GROUP BY tv_main.vesselName;
subquery returns more than 1 row ? sulution ?
SELECT `t_files`.*, `t_users`.`username`,
(SELECT CONCAT(b.first_name, " ", b.last_name)
FROM t_files AS a
JOIN t_users as b
ON b.id = a.user_id
) as upload_by
FROM `t_files`
LEFT JOIN `t_files_permission`
ON `t_files_permission`.`id_files` = `t_files`.`file_id`
LEFT JOIN `t_users`
ON `t_users`.`id` = `t_files_permission`.`id_users`
WHERE `t_files`.`company_id` = '1'
AND `t_files_permission`.`id_users` = '59'
AND `is_deleted` =0
Add the condition in your sub query like below
SELECT t_files.*, t_users.username,
(SELECT CONCAT(b.first_name, " ", b.last_name)
FROM t_files AS a
JOIN t_users AS b ON b.id = a.user_id
WHERE a.id_users` = '59'
) as upload_by
FROM t_files
LEFT JOIN t_files_permission ON t_files_permission.id_files = t_files.file_id
LEFT JOIN t_users ON t_users.id = t_files_permission.id_users
WHERE t_files.company_id = '1' AND t_files_permission.id_users = '59' AND is_deleted =0
try this way
SELECT `t_files`.*, `t_users`.`username`,
(select CONCAT(b.first_name, " ", b.last_name)
from t_files as a
join t_users as b on b.id = a.user_id
where t.files.id=a.id) as upload_by
FROM `t_files` as
LEFT JOIN `t_files_permission` ON `t_files_permission`.`id_files` = `t_files`.`file_id`
LEFT JOIN `t_users` ON `t_users`.`id` = `t_files_permission`.`id_users`
WHERE `t_files`.`company_id` = '1' AND `t_files_permission`.`id_users` = '59' AND `is_deleted` =0
I think you don't need sub query. You can use join to resolve stuff.
I want Mage_Adminhtml_Block_Dashboard_Orders_Grid setCollection rewrite.
I done :
public function setCollection($collection)
{
$connection = $collection->getConnection();
if(!Mage::helper("accountmanager")->isAdmin()){
$userId = Mage::helper("accountmanager")->getUserId();
$collection->getSelect()->joinInner(array('items_table'=>'sales_flat_order_item'),
'main_table.entity_id = items_table.order_id ',
null);
$collection->getSelect()->joinInner(array('e'=>'catalog_product_entity'),
'items_table.product_id = e.entity_id ',
null);
$collection->getSelect()->joinLeft(array('at_created_by'=>'catalog_product_entity_int'),
'(`at_created_by`.`entity_id` = `e`.`entity_id`) AND (`at_created_by`.`attribute_id` = 141) AND (`at_created_by`.`store_id` = 0) ',
null);
$collection->getSelect()->where("at_created_by.value = '".$userId."'");
}
exit($collection->getSelect());
parent::setCollection($collection);
}
result this:
SELECT `main_table`.*, `main_table`.`total_item_count` AS `items_count`, CONCAT_WS(' ', main_table.customer_firstname, main_table.customer_lastname) AS `customer`, `main_table`.`base_grand_total` AS `revenue` FROM `sales_flat_order` AS `main_table` INNER JOIN `sales_flat_order_item` AS `items_table` ON main_table.entity_id = items_table.order_id INNER JOIN `catalog_product_entity` AS `e` ON items_table.product_id = e.entity_id LEFT JOIN `catalog_product_entity_int` AS `at_created_by` ON (`at_created_by`.`entity_id` = `e`.`entity_id`) AND (`at_created_by`.`attribute_id` = 141) AND (`at_created_by`.`store_id` = 0) WHERE (store_id = '1') AND (at_created_by.value = '3')
this sql error : Error Code: 1052. Champ: 'store_id' dans where clause est ambigu
i think store_id no have alias 'main_table'.
How my query add alias??
please help me.
think you .
Having an issue with a specific section of a query using DATEDIFF:
select 1 as NumApps,
LenderInfo.Name as LenderName,
CASE WHEN ApplicationInfo.AEType IS NULL OR ApplicationInfo.AEType = 0 THEN 'Unknown' ELSECONCAT (AEContact.FirstName, ' ', AEContact.LastName) END As AEName,
CASE WHEN b.createdby > 0 THEN CONCAT (contactinfo.firstname, ' ', contactinfo.lastname) END AS LogActionBy,
CASE WHEN ApplicationInfo.RecertificationById IS NULL THEN CASE WHEN ApplicationInfo.IsCorrespondent IS NULL OR ApplicationInfo.IsCorrespondent = 0 THEN 'Wholesale' ELSE 'Correspondent' END ELSE CASE WHEN ApplicationInfo.IsCorrespondent IS NULL OR ApplicationInfo.IsCorrespondent = 0 THEN 'Wholesale - Recert' ELSE 'Correspondent- Recert' END END As AppType,
Applicationinfo.SubmissionDate,
ApplicationInfo.ApplicationID,
b.status AS LogStatus,
b.CreatedOn as LogDate,
companyinfo.Name,
companyinfo.NMLSEntityID,
applicationinfo.CreatedDate,
ApplicationInfo.Status as ApplicationStatus,
ApplicationInfo.StatusChangeDate,
DATEDIFF ((Select CreatedOn From ApplicationStatusChangeLog a where a.ApplicationId = b.ApplicationId
And a.status = 'Approved'),(Select CreatedOn From ApplicationStatusChangeLog a Where a.ApplicationId = b.ApplicationId And a.status = 'Pending Approval')) AS DaysToApprove
from applicationinfo
INNER JOIN CompanyInfo ON(ApplicationInfo.CompanyId = CompanyInfo.CompanyId)
left join CompanyInfo As LenderInfo ON (ApplicationInfo.LenderId = LenderInfo.CompanyId)
LEFT JOIN UserInfo ON (UserInfo.UserId = ApplicationInfo.LastModifiedById)
LEFT JOIN ContactInfo ON UserInfo.ContactId = ContactInfo.ContactId
LEFT JOIN ContactInfo AS Comergence ON(ApplicationInfo.ComergenceRepId = Comergence.ContactId)
LEFT JOIN UserInfo AS AEUser ON(AEUser.UserId = ApplicationInfo.AEType)
left join paymentinfo on applicationinfo.applicationid = paymentinfo.applicationid
Inner join applicationstatuschangelog b on applicationinfo.applicationid = b.applicationid
LEFT JOIN ContactInfo AS AEContact ON(AEContact.ContactId = AEUser.ContactId)
LEFT JOIN UserInfo AS StatusUser ON(StatusUser.UserId = ApplicationInfo.StatusChangeById)
LEFT JOIN ContactInfo AS StatusContact ON(StatusContact.ContactId = StatusUser.ContactId)
LEFT JOIN ApprovalStatus ON(ApplicationInfo.ApplicationId = ApprovalStatus.ApplicationId AND ApplicationInfo.CompanyId = ApprovalStatus.CompanyHQId
AND ApplicationInfo.CompanyId = ApprovalStatus.CompanyId)
Where ApplicationInfo.Status NOT In ('Approved Monitor Only')
AND LenderInfo.ActiveLenderContract = 1
AND COALESCE(ApplicationInfo.IsDeleted,0) <> 1
AND b.Status NOT IN ('Incomplete', 'Not Submitted')
Group BY b.ApplicationID`
I've seen some people mentioning using IN instead of = within the subquery, but I can't seem to get it to work. Any ideas out there? Thanks in advance.
I was able to get the query working by adding 'Limit 1' after the identified status:
(Select CreatedOn From ApplicationStatusChangeLog a where a.ApplicationId = b.ApplicationId
And a.status = 'Approved' LIMIT 1),(Select CreatedOn From ApplicationStatusChangeLog a Where a.ApplicationId = b.ApplicationId And a.status = 'Pending Approval' LIMIT 1)