SELECT *
FROM `tbl_equipments` as `E`
JOIN `tbl_equipment_category` as `C` ON `C`.`equipment_category_id`=`E`.`equipment_category_id`
JOIN `tbl_suppliers` as `S` ON `S`.`supplier_id`=`E`.`supplier_id`
JOIN `tbl_site_users` as `U` ON `U`.`user_id`=`E`.`created_by`
JOIN `tbl_currency` as `R` ON `R`.`currency_id`=`E`.`equipment_currency_id`
WHERE `E`.`site_id` = '3'
AND (SELECT *
FROM `tbl_user_delegates` as `D`
LEFT JOIN `tbl_equipments` as `E` ON `E`.`approve_by`=`D`.`delegate_from_user`
WHERE `D`.`delegate_from_date` <= '2016-10-27'
AND `D`.`delegate_to_date` >= '2016-10-27' AND `D`.`delegate_to_user` = '5'
ORDER BY `E`.`equipment_id` DESC)
AND (`E`.`approve_flg` =0 and `E`.`rejection_flg` =0) AND `E`.`approve_by` = '5'
Your AND ( subselect ) return * this is wrong
you should use exists
SELECT *
FROM `tbl_equipments` as `E`
JOIN `tbl_equipment_category` as `C` ON `C`.`equipment_category_id`=`E`.`equipment_category_id`
JOIN `tbl_suppliers` as `S` ON `S`.`supplier_id`=`E`.`supplier_id`
JOIN `tbl_site_users` as `U` ON `U`.`user_id`=`E`.`created_by`
JOIN `tbl_currency` as `R` ON `R`.`currency_id`=`E`.`equipment_currency_id`
WHERE `E`.`site_id` = '3'
AND EXISTS (SELECT *
FROM `tbl_user_delegates` as `D`
LEFT JOIN `tbl_equipments` as `E` ON `E`.`approve_by`=`D`.`delegate_from_user`
WHERE `D`.`delegate_from_date` <= '2016-10-27'
AND `D`.`delegate_to_date` >= '2016-10-27' AND `D`.`delegate_to_user` = '5'
ORDER BY `E`.`equipment_id` DESC)
AND (`E`.`approve_flg` =0 and `E`.`rejection_flg` =0) AND `E`.`approve_by` = '5'
adn Just a tips order by in select select is unuseful
Related
Hello I have this query generated by sequelize
`Orders` .*,
`OrderPriority`.`id` as `OrderPriority.ID`,
`OrderPriority`.`priority` as `OrderPriority.priority`,
`Employees`.`employee_alias` as `Employees.alias`,
`Employees`.`employee_id` as `Employees.ID` from
(
select
`Orders`.`id` as `ID`,
`Orders`.`due_date` as `dueDate`,
`Orders`.`creation_date` as `creationDate`,
`Orders`.`priority_id` as `priorityID`,
`OrderStatus`.`id` as `OrderStatus.ID`,
`OrderStatus`.`status` as `OrderStatus.status`
from
`orders` as `Orders`
inner join `statuses` as `OrderStatus` on
`Orders`.`status_id` = `OrderStatus`.`id`
where
(`Orders`.`creation_date` >= '2022-07-06 00:00:00'
and `Orders`.`creation_date` < '2022-07-14 00:00:00')
order by
`Orders`.`creation_date` desc
limit 0,
50) as `Orders`
left outer join `order_priorities` as `OrderPriority` on
`Orders`.`priorityID` = `OrderPriority`.`id`
left outer join ( `order_employees` as `Employees->OrderEmployees`
inner join `employes` as `Employees` on
`Employees`.`employee_id` = `Employees->OrderEmployees`.`employee_id`
and (`Employees->OrderEmployees`.`state` = 'Pending'
or `Employees->OrderEmployees`.`state` = 'Accepted')) on
`Orders`.`ID` = `Employees->OrderEmployees`.`order_id`
order by
`creationDate` desc;
I need to filter by null priority but the generated query has priority on the outer query if required:false which is logically wrong and the only way that I found to put the priority inside the subquery is to use required:true but this results in an inner join and I want left join in order to take the null values. Is there a way to force the include as left join inside the subquery?The nested subquery is generated because it exists a many to many relationship between Orders and Employees and limit must be applied to orders. I want to generate the following Query:
`Orders` .*,
`OrderPriority`.`id` as `OrderPriority.ID`,
`OrderPriority`.`priority` as `OrderPriority.priority`,
`Employees`.`employee_alias` as `Employees.alias`,
`Employees`.`employee_id` as `Employees.ID` from
(
select
`Orders`.`id` as `ID`,
`Orders`.`due_date` as `dueDate`,
`Orders`.`creation_date` as `creationDate`,
`Orders`.`priority_id` as `priorityID`,
`OrderStatus`.`id` as `OrderStatus.ID`,
`OrderStatus`.`status` as `OrderStatus.status`
from
`orders` as `Orders`
inner join `statuses` as `OrderStatus` on
`Orders`.`status_id` = `OrderStatus`.`id`
left outer join `order_priorities` as `OrderPriority` on
`Orders`.`priorityID` = `OrderPriority`.`id`
where
(`Orders`.`creation_date` >= '2022-07-06 00:00:00'
and `Orders`.`creation_date` < '2022-07-14 00:00:00' and `Orders`.`priorityID` is null)
order by
`Orders`.`creation_date` desc
limit 0,
50) as `Orders`
left outer join ( `order_employees` as `Employees->OrderEmployees`
inner join `employes` as `Employees` on
`Employees`.`employee_id` = `Employees->OrderEmployees`.`employee_id`
and (`Employees->OrderEmployees`.`state` = 'Pending'
or `Employees->OrderEmployees`.`state` = 'Accepted')) on
`Orders`.`ID` = `Employees->OrderEmployees`.`order_id`
order by
`creationDate` desc;
I am having trouble executing this sql. I need to get the result and send it via JSON to my web page.
SELECT `produto`.`nome` `nome`, `estoque_movimento`.`data`, `estoque_movimento`.`qtd`, `estoque_movimento`.`motivo`, `marca`.`nome` `marca`, `tipo_produto`.`nome` `categoria`
FROM `estoque_movimento`
LEFT JOIN `produto` ON `produto`.`id` = `estoque_movimento`.`produto_id`
LEFT JOIN `tipo_produto` ON `tipo_produto`.`id` = `produto`.`tipo_id`
LEFT JOIN `marca` ON `marca`.`id` = `produto`.`marca_id`
WHERE `estoque_movimento`.`data` >= '2018-03-26 00:00:00'
AND `estoque_movimento`.`data` <= '2018-10-03 23:59:59'
ORDER BY `estoque_movimento`.`data` DESC
UNION
SELECT `produto`.`nome` `nome`, `pedido_produto`.`criado_em`, `pedido_produto`.`qtde`, CONCAT("Alocado no pedido n°: ", pedido_produto.produto_id), `marca`.`nome` `marca`, `tipo_produto`.`nome` `categoria`
FROM `pedido_produto`
LEFT JOIN `pedido` ON `pedido`.`id` = `pedido_produto`.`pedido_id`
LEFT JOIN `produto` ON `produto`.`id` = `pedido_produto`.`produto_id`
LEFT JOIN `tipo_produto` ON `tipo_produto`.`id` = `produto`.`tipo_id`
LEFT JOIN `marca` ON `marca`.`id` = `produto`.`marca_id`
WHERE `pedido`.`is_finalizado` = 0
GROUP BY `produto`.`nome`, `pedido_produto`.`produto_id`
I am getting this error:
#1221 - Incorrect usage of UNION and ORDER BY
Use parantheses ( ) around different Select query groups.
Try:
(
SELECT `produto`.`nome` `nome`, `estoque_movimento`.`data`, `estoque_movimento`.`qtd`, `estoque_movimento`.`motivo`, `marca`.`nome` `marca`, `tipo_produto`.`nome` `categoria`
FROM `estoque_movimento`
LEFT JOIN `produto` ON `produto`.`id` = `estoque_movimento`.`produto_id`
LEFT JOIN `tipo_produto` ON `tipo_produto`.`id` = `produto`.`tipo_id`
LEFT JOIN `marca` ON `marca`.`id` = `produto`.`marca_id`
WHERE `estoque_movimento`.`data` >= '2018-03-26 00:00:00'
AND `estoque_movimento`.`data` <= '2018-10-03 23:59:59'
ORDER BY `estoque_movimento`.`data` DESC
)
UNION
(
SELECT `produto`.`nome` `nome`, `pedido_produto`.`criado_em`, `pedido_produto`.`qtde`, CONCAT("Alocado no pedido n°: ", pedido_produto.produto_id), `marca`.`nome` `marca`, `tipo_produto`.`nome` `categoria`
FROM `pedido_produto`
LEFT JOIN `pedido` ON `pedido`.`id` = `pedido_produto`.`pedido_id`
LEFT JOIN `produto` ON `produto`.`id` = `pedido_produto`.`produto_id`
LEFT JOIN `tipo_produto` ON `tipo_produto`.`id` = `produto`.`tipo_id`
LEFT JOIN `marca` ON `marca`.`id` = `produto`.`marca_id`
WHERE `pedido`.`is_finalizado` = 0
GROUP BY `produto`.`nome`, `pedido_produto`.`produto_id`
)
I have one query where I need to have number of rows of an table by particular ID:
SELECT
(CAST(`ot`.`value` AS DECIMAL(6,2))) AS `value`,
`op`.`orders_id`,
(
SELECT
COUNT(1) AS `total`
FROM
(
SELECT
`op2`.`concert_date`
FROM
`orders_products` `op2`
WHERE
`op2`.`orders_id` = `op`.`orders_id`
AND
`op2`.`concert_date` <> ''
GROUP BY CONCAT(`op2`.`concert_date`,' ',`op2`.`concert_time`)
) AS `e`
) AS `devider`
FROM
`categories` `c`
JOIN `products` `p` ON `p`.`section_id` = `c`.`section_id`
JOIN `orders_products` `op` ON `op`.`products_id` = `p`.`products_id`
JOIN `orders_total` `ot` ON `ot`.`orders_id` = `op`.`orders_id`
WHERE
`c`.`section_id` = 25
AND
`p`.`product_type` IN ('P')
AND
`ot`.`class` IN ('ot_shipping')
GROUP BY `op`.`orders_id`
The main problem is that I getting error
#1054 - Unknown column 'op.orders_id' in 'where clause'
And can't run this. I have separated query in my loop but that made performance issue and want to push it in one query. Any idea?
try removing sub-sub query and use COUNT(DISTINCT ..)
SELECT
(CAST(`ot`.`value` AS DECIMAL(6,2))) AS `value`,
`op`.`orders_id`,
(
SELECT
COUNT(DISTINCT CONCAT(`op2`.`concert_date`,' ',`op2`.`concert_time`))
FROM
`orders_products` `op2`
WHERE
`op2`.`orders_id` = `op`.`orders_id`
AND
`op2`.`concert_date` <> ''
) AS `devider`
FROM
`categories` `c`
JOIN `products` `p` ON `p`.`section_id` = `c`.`section_id`
JOIN `orders_products` `op` ON `op`.`products_id` = `p`.`products_id`
JOIN `orders_total` `ot` ON `ot`.`orders_id` = `op`.`orders_id`
WHERE
`c`.`section_id` = 25
AND
`p`.`product_type` IN ('P')
AND
`ot`.`class` IN ('ot_shipping')
GROUP BY `op`.`orders_id`
and you don't even need subquery or concat as long concert_date or concert_time is null
SELECT
(CAST(`ot`.`value` AS DECIMAL(6,2))) AS `value`,
`op`.`orders_id`,
COUNT(DISTINCT `op`.`concert_date`, `op`.`concert_time`) AS `devider`
FROM
`categories` `c`
JOIN `products` `p` ON `p`.`section_id` = `c`.`section_id`
LEFT JOIN `orders_products` `op` ON `op`.`products_id` = `p`.`products_id`
JOIN `orders_total` `ot` ON `ot`.`orders_id` = `op`.`orders_id`
WHERE
`c`.`section_id` = 25
AND
`p`.`product_type` IN ('P')
AND
`ot`.`class` IN ('ot_shipping')
GROUP BY `op`.`orders_id`
In this subquery:
SELECT
`op2`.`concert_date`
FROM
`orders_products` `op2`
WHERE
`op2`.`orders_id` = `op`.`orders_id`
AND
`op2`.`concert_date` <> ''
GROUP BY CONCAT(`op2`.`concert_date`,' ',`op2`.`concert_time`)
you are not selecting op as table.
Should be:
SELECT
`op2`.`concert_date`
FROM
`orders_products` `op2`, `orders_products` `op`
WHERE
`op2`.`orders_id` = `op`.`orders_id`
AND
`op2`.`concert_date` <> ''
GROUP BY CONCAT(`op2`.`concert_date`,' ',`op2`.`concert_time`)
Beside this you are making an implicit join while you should use explicit even here and the subquery should become:
SELECT
`op2`.`concert_date`
FROM
`orders_products` `op2` JOIN `orders_products` `op`
ON
`op2`.`orders_id` = `op`.`orders_id`
WHERE
`op2`.`concert_date` <> ''
GROUP BY CONCAT(`op2`.`concert_date`,' ',`op2`.`concert_time`)
mysql query:
`SELECT IFNULL(party.`user_id`, 0) as isparticipant, a.`id`, a.`created`,` `a.`lastreplied`,`
a.`type`, b.`created_by`, b.`message`, c.`isread`
FROM `jos_social_conversations` AS `a`
LEFT JOIN `jos_social_conversations_participants` as party on a.id = party.conversation_id
and party.user_id = '602'
INNER JOIN `jos_social_conversations_message` AS `b` ON
`a`.`id` = `b`.`conversation_id`
INNER JOIN `jos_social_conversations_message_maps` AS `c`
ON `c`.`message_id` = `b`.`id` and c.`conversation_id` = b.`conversation_id`
INNER JOIN
(select cm.`conversation_id`, max(cm.`message_id`) as `message_id` from
`jos_social_conversations_message_maps` as cm
inner join `jos_social_conversations_message` as bm
on cm.`message_id` = bm.`id`
LEFT JOIN `jos_social_block_users` AS `bus` ON `bm`.`created_by` = `bus`.`user_id`
AND `bus`.`target_id` = '602'
WHERE `cm`.`user_id` = '602' AND(SELECT count(isread) AS newMsg
FROM jos_social_conversations_message_maps as maps WHERE a.id = maps.conversation_id AND
maps.isread = 0 AND maps.user_id = '602') AND `cm`.`state` = '1' and `bus`.`id` IS NULL
group by cm.`conversation_id`) as x ON c.`message_id` = x.`message_id`
LEFT JOIN `jos_social_block_users`
as bus ON a.`created_by` = bus.`user_id` AND bus.`target_id` = '602'
WHERE `c`.`user_id` = '602'
AND bus.`id` IS NULL AND `c`.`state` = '1'
AND this gives error like:
1054 - Unknown column 'a.id' in 'where clause'
subquery from above query as below:
AND(SELECT count(isread) AS newMsg FROM jos_social_conversations_message_maps WHERE conversation_id = 3 AND isread = 0 AND user_id = '602')
Current output:
Expected output:
There should be another column called 'newMsg' displaying total count of 'isread' column which has '0' as value for each id..
Here's my formatted version:
SELECT IFNULL(party.`user_id`, 0) as isparticipant, a.`id`, a.`created`, a.`lastreplied`, a.`type`, b.`created_by`, b.`message`, c.`isread`
FROM `jos_social_conversations` AS `a`
LEFT JOIN `jos_social_conversations_participants` as party on a.id = party.conversation_id and party.user_id = '602'
INNER JOIN `jos_social_conversations_message` AS `b` ON `a`.`id` = `b`.`conversation_id`
INNER JOIN `jos_social_conversations_message_maps` AS `c` ON `c`.`message_id` = `b`.`id` and c.`conversation_id` = b.`conversation_id`
INNER JOIN (select cm.`conversation_id`, max(cm.`message_id`) as `message_id`
from `jos_social_conversations_message_maps` as cm
inner join `jos_social_conversations_message` as bm on cm.`message_id` = bm.`id`
LEFT JOIN `jos_social_block_users` AS `bus` ON `bm`.`created_by` = `bus`.`user_id` AND `bus`.`target_id` = '602'
WHERE `cm`.`user_id` = '602' AND(SELECT count(isread) AS newMsg
FROM jos_social_conversations_message_maps
WHERE conversation_id = 3 AND isread = 0 AND user_id = '602'
)
AND `cm`.`state` = '1'
and `bus`.`id` IS NULL
group by cm.`conversation_id`
) as x ON c.`message_id` = x.`message_id`
LEFT JOIN `jos_social_block_users` as bus ON a.`created_by` = bus.`user_id` AND bus.`target_id` = '602'
WHERE `c`.`user_id` = '602'
AND bus.`id` IS NULL AND `c`.`state` = '1'
Your subquery is part of the WHERE clause, so it will not return another column. Perhaps you are lookig for something like this:
SELECT IFNULL(party.`user_id`, 0) as isparticipant, a.`id`, a.`created`, a.`lastreplied`, a.`type`, b.`created_by`, b.`message`, c.`isread`,
(SELECT count(isread)
FROM jos_social_conversations_message_maps
WHERE conversation_id = 3 AND isread = 0 AND user_id = '602'
) AS newMsg
FROM `jos_social_conversations` AS `a`
{remainder removed for brevity}
Say I have a database similar to the following:
Table events_degree:
event_id degree_id
1 1
1 31
... ...
Table events_area:
event_id area_id
1 1
1 31
... ...
Table events_schedule:
event_id schedule
1 Time 1
1 Time 2
... ...
Table events:
id name
1 prom
2 homecoming
... ...
Table degree:
id name shortened
1 computer science cs
2 something else se
... ... ...
Table area:
id name
1 hall 1
2 gym
... ...
Table building:
id name
1 main building
2 second building
... ...
What i want to do it's merge the columns since i have this query
SELECT `e`.*, `eh`.`start_date`, `eh`.`end_date`, `c`.`name` AS `degree_name`, `c`.`shortened` AS `shortened_degree`, `ai`.`name` AS `area_name`, `ed`.`name` AS `building_name`
FROM `events` `e`
LEFT JOIN `event_schedule` `eh` ON `e`.`id` = `eh`.`event_id`
LEFT JOIN `event_degree` `ec` ON `e`.`id` = `ec`.`event_id`
LEFT JOIN `degree` `c` ON `c`.`id` = `ec`.`degree_id`
LEFT JOIN `event_area` `ea` ON `e`.`id` = `ea`.`event_id`
LEFT JOIN `area` `ai` ON `ai`.`id` = `ea`.`area_id`
LEFT JOIN `building` `ed` ON `ed`.`id` = `ai`.`building_id`
WHERE `e`.`active` = '1'
AND `eh`.`start_date` >= '2016-03-08'
AND `eh`.`end_date` < '2016-07-01'
ORDER BY `eh`.`fecha_inicio` ASC;
and as a result i get all the rows "duplicated" as you can see in the picture
how can i avoid this, thanks for your help
** note **
it's the same event so it should be displaying only 1 row for every event the problem is that i has 3 many to many relationships
so the things that change are only dates, degrees and areas
You can use GROUP BY
SELECT DISTINCT `e`.*, `eh`.`start_date`, `eh`.`end_date`, `c`.`name` AS `degree_name`, `c`.`shortened` AS `shortened_degree`, `ai`.`name` AS `area_name`, `ed`.`name` AS `building_name`
FROM `events` `e`
LEFT JOIN `event_schedule` `eh` ON `e`.`id` = `eh`.`event_id`
LEFT JOIN `event_degree` `ec` ON `e`.`id` = `ec`.`event_id`
LEFT JOIN `degree` `c` ON `c`.`id` = `ec`.`degree_id`
LEFT JOIN `event_area` `ea` ON `e`.`id` = `ea`.`event_id`
LEFT JOIN `area` `ai` ON `ai`.`id` = `ea`.`area_id`
LEFT JOIN `building` `ed` ON `ed`.`id` = `ai`.`building_id`
WHERE `e`.`active` = '1'
AND `eh`.`start_date` >= '2016-03-08'
AND `eh`.`end_date` < '2016-07-01'
GROUP BY `your_id`;
Use DISTINCT while selecting
SELECT DISTINCT `e`.*, `eh`.`start_date`, `eh`.`end_date`, `c`.`name` AS `degree_name`, `c`.`shortened` AS `shortened_degree`, `ai`.`name` AS `area_name`, `ed`.`name` AS `building_name`
FROM `events` `e`
LEFT JOIN `event_schedule` `eh` ON `e`.`id` = `eh`.`event_id`
LEFT JOIN `event_degree` `ec` ON `e`.`id` = `ec`.`event_id`
LEFT JOIN `degree` `c` ON `c`.`id` = `ec`.`degree_id`
LEFT JOIN `event_area` `ea` ON `e`.`id` = `ea`.`event_id`
LEFT JOIN `area` `ai` ON `ai`.`id` = `ea`.`area_id`
LEFT JOIN `building` `ed` ON `ed`.`id` = `ai`.`building_id`
WHERE `e`.`active` = '1'
AND `eh`.`start_date` >= '2016-03-08'
AND `eh`.`end_date` < '2016-07-01'
ORDER BY `eh`.`fecha_inicio` ASC;