How to use UNION with two selects and some JOINS - mysql

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`
)

Related

Sequelize: Force left join on nested subquery which is produced by limit

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;

How can I speed up this query with mysqli?

I have this query and it take 117 seconds to get 100000 rows can me get all this rows on 5 seconds or 10 seconds?
SELECT
`nas`.`nasname`,
`nas`.`api_username`,
`nas`.`api_password`,
`nas`.`api_port`,
`nas`.`secret`,
`nas`.`up_by`,
COALESCE(`nas`.`api_is_enabled`,'0') as `api_is_en`,
`radacct`.`nasipaddress`,
`radcheck`.`address_list_name`,
`radcheck`.`address_val`,
`radacct`.`framedipaddress`,
`radacct`.`callingstationid`,
`radacct`.`radacctid` as `id_session`,
`radacct`.`framedprotocol`,
`radacct`.`username`,
`radacct`.`last_speed` ,
`radacct`.`acctsessionid` as `acctsessionid`,
`radip`.`value` as `ip_address`,
`radmac`.`value` as `mac_address`,
`radcount`.`value` as `simul_sess`,
COUNT(`radacct`.`radacctid`) as count_login ,
COALESCE(`card_users`.`id`,0) as `id_card`,
COALESCE(`card_users`.`download_qouta`,`userinfo`.`in_down`) *1024*1024 as `down_qouta`,
COALESCE(`card_users`.`upload_qouta`,`userinfo`.`in_up`) *1024*1024 as `up_qouta`,
COALESCE(`card_users`.`all_quota`,`userinfo`.`av_qouta`) *1024*1024 as `av_qouta`,
`card_users`.`date_end_card` as `date_end_card`,
`card_users`.`val_date` as `val_date`,
`card_users`.`per_second` as `per_second`,
`card_users`.`at_the_first_login` as `at_the_first_login`,
`card_users`.`exp_first_login` as `exp_first_login`,
`card_users`.`val_time_exp` as `val_time_exp`,
`card_users`.`time_cards_exp` as `time_cards_exp`,
`card_users`.`exp_quota` as `exp_quota`,
`card_users`.`exp_date` as `exp_date`,
`userinfo`.`updatedate` as `updatedate`,
`userinfo`.`divi_down_speed_slm` as `divi_down_speed_slm`,
`userinfo`.`divi_up_speed_slm` as `divi_up_speed_slm`,
`userinfo`.`arr_days` as `u_arr_days`,
`userinfo`.`value_choice` as `u_value_choice`,
`userinfo`.`last_end_day` as `last_end_day`,
`userinfo`.`macs` as `macs`,
`p`.`profile_name` as `profile_name`,
`p`.`daily_down_qouta`*1024*1024 as `down_daily_qouta`,
`p`.`daily_up_qouta`*1024*1024 as `up_daily_qouta`,
`p`.`daily_profile_qouta`*1024*1024 as `daily_profile_qouta`,
`p`.`online_time` as `online_time`,
`p`.`hours_min` as `hours_min`,
`p`.`daily_online_time` as `daily_online_time`,
`p`.`daily_hours_min` as `daily_hours_min`,
`p`.`bandwidth_time` as `bandwidth_time`,
`p`.`daily_expire_service` as `daily_expire_service`,
`p`.`qouta_expire_service` as `qouta_expire_service`,
`p`.`profile_expire_service` as `profile_expire_service`,
`p`.`sp_up` as `sp_up`,
`p`.`sp_down` as `sp_down`,
`p`.`ch_day_end` as `ch_day_end`,
`p`.`set_day_end` as `set_day_end`,
`p`.`percent` as `percent`,
`p`.`arr_days` as `p_arr_days`,
`p`.`value_choice` as `p_value_choice`,
`r1`.`value` as `value_exp`,
`p1`.`bandwidth_time` as `band_exp`,
`p1`.`percent` as `percent_exp`,
`r2`.`value` as `val_address_exp_list`,
`r3`.`value` as `value_exp_daily`,
`p3`.`bandwidth_time` as `band_daily_exp`,
`p3`.`percent` as `percent_daily_exp`,
`r4`.`value` as `val_address_exp_daily_list`,
`r5`.`value` as `value_exp_serv`,
`p3`.`bandwidth_time` as `band_serv_exp`,
`p3`.`percent` as `percent_serv_exp`,
`r6`.`value` as `val_address_exp_serv_list`,
`r7`.`value` as `value_now`,
`r8`.`value` as `val_address_list`,
COALESCE(ROUND(time_to_sec(`userinfo`.`online_time`)),0) as `t_online_time` ,
COALESCE(ROUND(time_to_sec(`userinfo`.`daily_online_time`)),0) as `d_online_time`,
UNIX_TIMESTAMP(STR_TO_DATE(`radexp`.`value`, '%d %b %Y %H:%i')) as `exp_user`,
`radacct`.`callingstationid` as `callingstationid`,
UNIX_TIMESTAMP(STR_TO_DATE(`radacct`.`acctstarttime`, '%Y-%m-%d %H:%i:%s')) as `session_st_date`
FROM `radacct`
INNER JOIN `radcheck`
ON `radacct`.`username` = `radcheck`.`username`
AND `radcheck`.`attribute` = 'Cleartext-Password'
INNER JOIN `radusergroup` `r9`
ON `r9`.`username` = `radcheck`.`username`
LEFT JOIN `userinfo`
ON `userinfo`.`username` = `r9`.`username`
LEFT JOIN (
SELECT SUM(`radacct`.`acctinputoctets`) as `up_today`
, SUM(`radacct`.`acctoutputoctets`) as `down_today`
, SUM(`radacct`.`acctsessiontime`) as `daily_time`
, `radacct`.`username`
FROM `radacct`
WHERE DATE_FORMAT(STR_TO_DATE(`acctstarttime`,'%Y-%m-%d %H:%i:%s'),'%Y-%m-%d') = DATE_FORMAT(NOW(),'%Y-%m-%d')
GROUP BY `username`
) as `rad2`
ON `rad2`.`username` = `r9`.`username`
INNER JOIN `nas`
ON `nas`.`nasname`=`radacct`.`nasipaddress`
LEFT JOIN `profiles` `p`
ON `p`.`profile_name` = `r9`.`groupname`
LEFT JOIN `profiles` `p1`
ON `p1`.`id` = `p`.`qouta_expire_service`
LEFT JOIN `profiles` `p2`
ON `p2`.`id` = `p`.`daily_expire_service`
LEFT JOIN `profiles` `p3`
ON `p3`.`id` = `p`.`profile_expire_service`
LEFT JOIN `radgroupreply` `r1`
ON `r1`.`groupname` = `p1`.`profile_name`
AND `r1`.`attribute` = 'Mikrotik-Rate-Limit'
LEFT JOIN `radgroupreply` `r2`
ON `r2`.`groupname` = `p1`.`profile_name`
AND `r2`.`attribute` = 'Mikrotik-Address-List'
LEFT JOIN `radgroupreply` `r3`
ON `r3`.`groupname` = `p2`.`profile_name`
AND `r3`.`attribute` = 'Mikrotik-Rate-Limit'
LEFT JOIN `radgroupreply` `r4`
ON `r4`.`groupname` = `p2`.`profile_name`
AND `r4`.`attribute` = 'Mikrotik-Address-List'
LEFT JOIN `radgroupreply` `r5`
ON `r5`.`groupname` = `p3`.`profile_name`
AND `r5`.`attribute` = 'Mikrotik-Rate-Limit'
LEFT JOIN `radgroupreply` `r6`
ON `r6`.`groupname` = `p3`.`profile_name`
AND `r6`.`attribute` = 'Mikrotik-Address-List'
LEFT JOIN `radgroupreply` `r7`
ON `r7`.`groupname` = `p`.`profile_name`
AND `r7`.`attribute` = 'Mikrotik-Rate-Limit'
LEFT JOIN `radgroupreply` `r8`
ON `r8`.`groupname` = `p`.`profile_name`
AND `r8`.`attribute` = 'Mikrotik-Address-List'
LEFT JOIN `card_users`
ON `card_users`.`id` = `radcheck`.`id_card`
LEFT JOIN `radreply` as `radip`
ON `radip`.`username` = `r9`.`username`
AND `radip`.`attribute`='Framed-IP-Address'
LEFT JOIN `radcheck` as `radmac`
ON `radmac`.`username` = `r9`.`username`
AND `radmac`.`attribute`='Calling-Station-Id'
LEFT JOIN `radcheck` as `radcount`
ON `radcount`.`username` = `r9`.`username`
AND `radcount`.`attribute`='Simultaneous-Use'
LEFT JOIN `radcheck` as `radexp`
ON `radexp`.`username` = `r9`.`username`
AND `radexp`.`attribute`='Expiration'
WHERE ( radacct .AcctStopTime IS NULL
OR radacct.AcctStopTime = '0000-00-00 00:00:00')
GROUP BY `radacct`.`username`
When remove this from query it speed up 60%
LEFT JOIN (SELECT SUM(`radacct`.`acctinputoctets`) as `up_today`,
SUM(`radacct`.`acctoutputoctets`) as `down_today`,
SUM(`radacct`.`acctsessiontime`) as `daily_time`,
`radacct`.`username` FROM `radacct`
WHERE DATE_FORMAT(STR_TO_DATE(`acctstarttime`,'%Y-%m-%d %H:%i:%s'),'%Y-%m-%d') = DATE_FORMAT(NOW(),'%Y-%m-%d')
GROUP BY `username`) as `rad2` ON (`rad2`.`username` = `r9`.`username`)
I have index for all this tables so what is my problem ?
Note : I have pc have
cpu core i 5
ram 4 giga
this image for explain my table

How to create codeigniter query from below sql query?

I've a query like this which is working fine but I want to create a pure CI query from this. I'm new in CI so can anyone help me to figure out how this can be done or suggest me a reference website or link from where i can get help. Thanks
SELECT
user_des,
IFNULL((sent),0) as sent,
IFNULL((viewed),0) as viewed,
CONCAT(IFNULL(fname_usr,user_des),' ',lname_usr,user_des) as fullname,
IFNULL(sum(duration),0) as totalviewtime,
IFNULL((totalviews),0) as views,
IFNULL(sum(sharedcount),0) as shares,
IFNULL(sum(shared),0) as shared
FROM dem_senddemo
INNER JOIN
(SELECT * FROM dem_demos WHERE id_dem IN (746,943,748) AND customer_dem = '1') as demosfiltered
ON demo_des = id_dem
LEFT JOIN
(
(
Select senddemo_wis, sum(duration) as duration, max(datereceived_wis) as datereceived_wis
FROM
(
select senddemo_wis, invited_wis, sum(IFNULL(duration_fea,0) * IFNULL(percentviewed_wis,0)) as duration, max(datereceived_wis) as datereceived_wis
FROM (sta_views
LEFT JOIN sta_featureswisitavisits as s ON visit_fvi = id_vis
LEFT JOIN
(
(SELECT shortvideovid_fea as videoid_fea, shortvideoduration_fea as duration_fea
FROM dem_features
where shortvideoduration_fea > 0)
UNION
(SELECT longvideovid_fea as videoid_fea, longvideoduration_fea as duration_fea
FROM dem_features
where longvideoduration_fea > 0)
) as x
ON videoid_fea = showedvideo_fvi)
LEFT JOIN
sta_wistia as w ON invited_fvi = invited_wis AND s.showedvideo_fvi = w.mediaid_wis AND wistia_vis = email_wis
WHERE countvisit_wis <> 'N'
GROUP BY invited_wis
) as durfea
GROUP BY senddemo_wis
) UNION
(
SELECT senddemo_wis,sum(IFNULL(mainvideoduration_dem ,0) * IFNULL(percentviewed_wis,0)) as duration, max(datereceived_wis) as datereceived_wis
FROM sta_wistia as w
INNER JOIN
(select *, customer_dem as cuss FROM dem_demos) as demcorp ON demo_wis = id_dem AND mainmessagingvideovid_dem = w.mediaid_wis
LEFT JOIN sta_views ON email_wis = wistia_vis
WHERE customer_dem = 1 AND senddemo_wis > 0 AND countvisit_wis <> 'N'
GROUP BY senddemo_wis
)
) as c ON id_des = senddemo_wis
LEFT JOIN
(
SELECT user_des as user2_des, count(id_vis) as totalviews
FROM sta_views
LEFT JOIN dem_invited ON id_invited = invited_vis
LEFT JOIN dem_senddemo ON id_des = id_senddemo
WHERE NOT ISNULL(id_senddemo) AND countvisit_wis != 'N' AND createdon_des BETWEEN '2015-05-31 00:00:00' AND '2015-06-29 23:59:59' AND demo_des IN (746,943,748)
GROUP BY user_des
) as z ON user_des = user2_des
LEFT JOIN
(
SELECT user_des as user3_des, count(id_des) as sent
FROM dem_senddemo
LEFT JOIN dem_invited ON id_senddemo = id_des
WHERE createdon_des BETWEEN '2015-05-31 00:00:00' AND '2015-06-29 23:59:59' AND demo_des IN (746,943,748) AND first_invited = 'YES'
GROUP BY user_des
) as za ON user_des = user3_des
LEFT JOIN
(
SELECT user_des as user4_des, count(id_vis) as viewed
FROM sta_views LEFT JOIN dem_invited ON id_invited = invited_vis
LEFT JOIN dem_senddemo ON id_des = id_senddemo
WHERE NOT ISNULL(id_senddemo) AND countvisit_wis != 'N' AND createdon_des BETWEEN '2015-05-31 00:00:00' AND '2015-06-29 23:59:59' AND state = 'VIEWED'
AND first_invited = 'YES' AND demo_des IN (746,943,748)
GROUP BY user_des
) as zb ON user_des = user4_des
LEFT JOIN
(
SELECT id_senddemo as iddemdemos2, count(id_senddemo) as shared, allshares as sharedcount
FROM
(
SELECT id_senddemo, count(id_senddemo) as allshares
FROM dem_invited
LEFT JOIN dem_senddemo on id_senddemo = id_des
WHERE first_invited <> 'YES' AND demo_des IN (746,943,748)
GROUP BY id_senddemo
) as x
GROUP BY id_senddemo
) as zc ON iddemdemos2 = id_des
LEFT JOIN
sec_users ON user_des = id_usr
WHERE customer_dem = 1 AND createdon_des BETWEEN '2015-05-31 00:00:00' AND '2015-06-29 23:59:59'
GROUP BY user_des

Only getting one result from sub select in inner join in mysql

Hi I have run into a dilemma, I am doing this query:
SELECT GROUP_CONCAT(DISTINCT(ca.category_name) SEPARATOR ', ') AS categories, pr.promo_name, pr.add_value_text, c.contract_id, c.cmeal_plan, c.cmin_markup, c.civa, c.tax_include, c.hotel_id, hi.hname, hi.hstars, im.image_file, pl.plan_name, ra.price
FROM contracts AS c
INNER JOIN hotel_info AS hi ON hi.hotel_id = c.hotel_id AND hi.destination_id = '6460'
INNER JOIN images AS im ON im.foreign_id = hi.hotel_id
INNER JOIN meal_plan AS pl ON pl.plan_code = c.cmeal_plan AND pl.lang = '1'
INNER JOIN hotel_categories AS hc ON hc.hotel_id = hi.hotel_id
INNER JOIN categories AS ca ON ca.category_code = hc.category_code AND ca.lang = '1'
LEFT JOIN
(SELECT
r.hotel_id, AVG(r.double) AS price
FROM
rates AS r ) AS ra
ON ra.hotel_id = hi.hotel_id
LEFT JOIN promotions AS pr ON pr.hotel_id = hi.hotel_id AND FIND_IN_SET(c.contract_id, pr.contract_id) > 0 AND pr.book_start <= '2012-11-01' AND pr.book_end >= '2012-11-02' AND travel_start <= '2012-11-23' AND travel_end >= '2012-11-30' AND pr.lang = '1'
WHERE c.cstart <= '2012-11-01' AND c.cend >= '2012-11-01'
AND hi.status = '1'
AND im.type ='1'
GROUP BY hi.hotel_id
I am getting all the desired results except for the sub select query.. each hotel has a price but it is only giving me back one result and the rest are all null. Is there an error in my query? If any additional information is needed please let me know and thank you in advance for any help!
You are missing the GROUP BY in your subquery, so MySQL will only return one-value. If you want all hotel_id's then you need to GROUP BY that field:
SELECT GROUP_CONCAT(DISTINCT(ca.category_name) SEPARATOR ', ') AS categories,
pr.promo_name,
pr.add_value_text,
c.contract_id,
c.cmeal_plan,
c.cmin_markup,
c.civa,
c.tax_include,
c.hotel_id,
hi.hname,
hi.hstars,
im.image_file,
pl.plan_name,
ra.price
FROM contracts AS c
INNER JOIN hotel_info AS hi
ON hi.hotel_id = c.hotel_id
AND hi.destination_id = '6460'
INNER JOIN images AS im
ON im.foreign_id = hi.hotel_id
INNER JOIN meal_plan AS pl
ON pl.plan_code = c.cmeal_plan
AND pl.lang = '1'
INNER JOIN hotel_categories AS hc
ON hc.hotel_id = hi.hotel_id
INNER JOIN categories AS ca
ON ca.category_code = hc.category_code
AND ca.lang = '1'
LEFT JOIN
(
SELECT r.hotel_id, AVG(r.double) AS price
FROM rates AS r
GROUP BY r.hotel_id <-- add a GROUP BY hotel_id then you will get avg() for each hotel
) AS ra
ON ra.hotel_id = hi.hotel_id
LEFT JOIN promotions AS pr
ON pr.hotel_id = hi.hotel_id
AND FIND_IN_SET(c.contract_id, pr.contract_id) > 0
AND pr.book_start <= '2012-11-01'
AND pr.book_end >= '2012-11-02'
AND travel_start <= '2012-11-23'
AND travel_end >= '2012-11-30'
AND pr.lang = '1'
WHERE c.cstart <= '2012-11-01'
AND c.cend >= '2012-11-01'
AND hi.status = '1'
AND im.type ='1'
GROUP BY hi.hotel_id

Suggestions on complex sql statement

SELECT `v`.*
, GROUP_CONCAT(DISTINCT(`gg`.genre_id)) AS `genre_ids`
, GROUP_CONCAT(DISTINCT(`gg`.genre_ru)) AS `genres`
, GROUP_CONCAT(DISTINCT(`tg`.tag_id)) AS `tags_ids`
, GROUP_CONCAT(DISTINCT(`tg`.name)) AS `tags`
FROM `video` AS `v`
LEFT JOIN `video_categories` AS `gc` ON gc.video_id = v.video_id
LEFT JOIN `video_genres` AS `gg` ON gg.genre_id = gc.genre_id
LEFT JOIN `tags_relations` AS `tr` ON tr.department=18 AND tr.parent_id = v.video_id
LEFT JOIN `tags` AS `tg` ON tg.tag_id = tr.tag_id
WHERE (v.video_id IN (
SELECT gc2.video_id
FROM video_categories gc2
WHERE gc2.genre_id IN(44)
GROUP BY gc2.video_id
HAVING COUNT(gc2.video_id)=1))
AND (v.video_id IN (
SELECT gc2.parent_id
FROM tags_relations gc2
WHERE gc2.tag_id IN(14) AND gc2.department=18
GROUP BY gc2.parent_id
HAVING COUNT(gc2.parent_id)=1))
GROUP BY `v`.`video_id`
ORDER BY `v`.`video_id` DESC
Query takes 0.4s~ to run. It's because of subquery in WHERE statement:
v.video_id IN (
SELECT gc2.parent_id
FROM tags_relations gc2
WHERE gc2.tag_id IN(14) AND gc2.department=18
GROUP BY gc2.parent_id
HAVING COUNT(gc2.parent_id)=1
)
Without it, it runs 0.07s~
Maybe try something with advanced joins? shy
Thanks ;)
In MySQL, never use WHERE in (SELECT ... it is notoriously slow.
Use a join instead:
SELECT `v`.*
, GROUP_CONCAT(DISTINCT(`gg`.genre_id)) AS `genre_ids`
, GROUP_CONCAT(DISTINCT(`gg`.genre_ru)) AS `genres`
, GROUP_CONCAT(DISTINCT(`tg`.tag_id)) AS `tags_ids`
, GROUP_CONCAT(DISTINCT(`tg`.name)) AS `tags`
FROM video AS v
INNER JOIN video_categories AS gc ON gc.video_id = v.video_id
AND gc2.genre_id = '44'
LEFT JOIN video_genres AS gg ON gg.genre_id = gc.genre_id
LEFT JOIN tags_relations AS tr ON tr.department=18 AND tr.parent_id = v.video_id
LEFT JOIN tags AS tg ON tg.tag_id = tr.tag_id
INNER JOIN tags_relations tr2 ON (tr2.parent_id = v.video_id
AND tr2.tag_id = 14 AND tr2.department=18
GROUP BY `v`.`video_id`
ORDER BY `v`.`video_id` DESC
SELECT `v`.*
, GROUP_CONCAT(DISTINCT(`gg`.genre_id)) AS `genre_ids`
, GROUP_CONCAT(DISTINCT(`gg`.genre_ru)) AS `genres`
, GROUP_CONCAT(DISTINCT(`tg`.tag_id)) AS `tags_ids`
, GROUP_CONCAT(DISTINCT(`tg`.name)) AS `tags`
FROM `video` AS `v`
LEFT JOIN `video_categories` AS `gc` ON gc.video_id = v.video_id
LEFT JOIN `video_genres` AS `gg` ON gg.genre_id = gc.genre_id
LEFT JOIN `tags_relations` AS `tr` ON tr.department=18 AND tr.parent_id = v.video_id
LEFT JOIN `tags` AS `tg` ON tg.tag_id = tr.tag_id
JOIN (
SELECT gc2.video_id
FROM video_categories gc2
WHERE gc2.genre_id IN(44)
GROUP BY gc2.video_id
HAVING COUNT(gc2.video_id)=1
) vc ON v.video_id = vc.video_id
JOIN (
SELECT gc2.parent_id
FROM tags_relations gc2
WHERE gc2.tag_id IN(14) AND gc2.department=18
GROUP BY gc2.parent_id
HAVING COUNT(gc2.parent_id)=1
) tr ON v.video_id = tr.parent_id
GROUP BY `v`.`video_id`
ORDER BY `v`.`video_id` DESC
Edit:
I took a closer look at your query and think it can be reduced to the following:
SELECT `v1`.*
, GROUP_CONCAT(DISTINCT(`gg`.genre_id)) AS `genre_ids`
, GROUP_CONCAT(DISTINCT(`gg`.genre_ru)) AS `genres`
, GROUP_CONCAT(DISTINCT(`tg`.tag_id)) AS `tags_ids`
, GROUP_CONCAT(DISTINCT(`tg`.name)) AS `tags`
FROM (
SELECT v.*
FROM `video` AS `v`
LEFT JOIN `video_categories` AS `gc` ON gc.video_id = v.video_id
LEFT JOIN `tags_relations` AS `tr` ON tr.parent_id = v.video_id
WHERE gc.genre_id IN(44)
AND tr.tag_id IN(14)
AND tr.department=18
GROUP BY `v`.`video_id`
HAVING COUNT(*) = 1
) v1
LEFT JOIN `video_genres` AS `gg` ON gg.genre_id = gc.genre_id
LEFT JOIN `tags` AS `tg` ON tg.tag_id = tr.tag_id
GROUP BY v1.video_id
ORDER BY v1.video_id DESC