Table rows into columns in mysql - mysql

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;

Related

how optimize prestashop category get product for random

this is prestashop 1.7 version category get product query. if use random, it is very slow, how optimize it?
SELECT
cp.id_category,
p.*,
product_shop.*,
stock.out_of_stock,
IFNULL( stock.quantity, 0 ) AS quantity,
IFNULL( product_attribute_shop.id_product_attribute, 0 ) AS id_product_attribute,
product_attribute_shop.minimal_quantity AS product_attribute_minimal_quantity,
pl.`description`,
pl.`description_short`,
pl.`available_now`,
pl.`available_later`,
pl.`link_rewrite`,
pl.`meta_description`,
pl.`meta_keywords`,
pl.`meta_title`,
pl.`name`,
image_shop.`id_image` id_image,
il.`legend` AS legend,
m.`name` AS manufacturer_name,
cl.`name` AS category_default,
DATEDIFF(
product_shop.`date_add`,
DATE_SUB( "2019-11-30 00:00:00", INTERVAL 7 DAY )) > 0 AS new,
product_shop.price AS orderprice
FROM
`ps_category_product` cp
LEFT JOIN `ps_product` p ON p.`id_product` = cp.`id_product`
INNER JOIN ps_product_shop product_shop ON ( product_shop.id_product = p.id_product AND product_shop.id_shop = 1 )
LEFT JOIN `ps_product_attribute_shop` product_attribute_shop ON ( p.`id_product` = product_attribute_shop.`id_product` AND product_attribute_shop.`default_on` = 1 AND product_attribute_shop.id_shop = 1 )
LEFT JOIN ps_stock_available stock ON ( stock.id_product = `p`.id_product AND stock.id_product_attribute = 0 AND stock.id_shop = 1 AND stock.id_shop_group = 0 )
LEFT JOIN `ps_category_lang` cl ON ( product_shop.`id_category_default` = cl.`id_category` AND cl.`id_lang` = 11 AND cl.id_shop = 1 )
LEFT JOIN `ps_product_lang` pl ON ( p.`id_product` = pl.`id_product` AND pl.`id_lang` = 11 AND pl.id_shop = 1 )
LEFT JOIN `ps_image_shop` image_shop ON ( image_shop.`id_product` = p.`id_product` AND image_shop.cover = 1 AND image_shop.id_shop = 1 )
LEFT JOIN `ps_image_lang` il ON ( image_shop.`id_image` = il.`id_image` AND il.`id_lang` = 11 )
LEFT JOIN `ps_manufacturer` m ON m.`id_manufacturer` = p.`id_manufacturer`
WHERE
product_shop.`id_shop` = 1
AND cp.`id_category` = 12
AND product_shop.`active` = 1
AND product_shop.`visibility` IN ( "both", "catalog" )
ORDER BY
RAND()
LIMIT 50
Please provide SHOW CREATE TABLE for each table. Meanwhile, ...
Let's start by optimizing the joins.
LEFT JOIN `ps_product_lang` pl ON ( p.`id_product` = pl.`id_product`
AND pl.`id_lang` = 11
AND pl.id_shop = 1 )
That needs INDEX(id_product, id_lang, id_shop) (The columns may be in any order.)
Don't use LEFT unless you really need to fetch a row from the righthand table as NULLs when it does not exist. In particular,
LEFT JOIN `ps_product` p
is probably getting in the way of optimization.
WHERE product_shop.`id_shop` = 1
AND product_shop.`active` = 1
AND product_shop.`visibility` IN ( "both", "catalog" )
would probably benefit from these indexes
INDEX(id_shop, active, visibility, id_product)
INDEX(id_product, id_shop, active, visibility)
product_category needs
INDEX(id_category, id_product) -- in this order.
In general many-to-many mapping tables need to follow the tips here: http://mysql.rjweb.org/doc.php/index_cookbook_mysql#many_to_many_mapping_table
The query has the "explode-implode" syndrome. This is where it first does a JOINs, collecting a lot of data, then throws away much of it due, in your case, to the LIMIT 10. It can probably be cured by turning the query inside-out. The general ID is to start with a derived table that gets the 10 rows desired, then reaches into the other table for the rest of the desired columns. This "reaching" need happen only 10 times, not however many the JOINs currently require.
SELECT ...
FROM ( SELECT <<primary key columns from cp, p, and product_shop>>
FROM cp
JOIN p ON ...
JOIN product_shop ON ...
ORDER BY RAND()
LIMIT 10 ) AS x
JOIN <<p, product_shop ON their PKs>> -- to get p.*, product_shop.*>>
[LEFT] JOIN << each of the other tables>> -- to get the other tables
You should start by testing the subquery (a "derived" table) to verify that it is noticeably faster than the original query.

multiple indexing with slow query

In following query I indexed every field like:
menu.id
pricelist.menu_id
vendors.id
pricelist.vendor
orders.pricelist_id
pricelist.id
users.id
orders.user_id
orders.free
pricelist.menu_id.
When I run the below query it takes much time, we have 13 million records in orders table and other table has few thousands.
SELECT
`orders`.`itusername`,
`orders`.`iturl`,
`orders`.`error_message`,
`orders`.`return` AS return1,
`orders`.`coupon`,
DATEDIFF( users.reseller_expiry, now( ) ) AS edays,
`users`.`email`,
`menu`.`menuname`,
`orders`.`error_status`,
`orders`.`auto_status`,
`vendors`.`name`,
`vendors`.`id` AS venderid,
`pricelist`.`servicename`,
`orders`.`email_order` AS paypal_order_email,
`orders`.`user_id`,
`orders`.`services_order`,
`orders`.`created_dt`,
`orders`.`id`,
`orders`.`transaction_comment`,
`orders`.`url`,
`orders`.`requireviews`,
`orders`.`youtubeviews`,
`orders`.`total_views_completed`,
`orders`.`aff`,
`orders`.`is_package`,
`orders`.`price`,
`orders`.`order_from_site`,
`orders`.`cost_per_unit_order`,
`orders`.`service_name_order`,
`orders`.`status`,
`orders`.`start_api_date`,
`orders`.`end_api_date`,
`orders`.`allow_setting`,
`orders`.`return2`,
`users`.`balance` AS user_balance
FROM
( `pricelist` )
JOIN `menu` ON `menu`.`id` = `pricelist`.`menu_id`
JOIN `vendors` ON `vendors`.`id` = `pricelist`.`vendor`
JOIN `orders` ON `orders`.`pricelist_id` = `pricelist`.`id`
JOIN `users` ON `users`.`id` = `orders`.`user_id`
WHERE
`orders`.`free` != 1
AND pricelist.menu_id = 3
ORDER BY
`orders`.`id` DESC
LIMIT 10.
.................................
The trick is to do the LIMIT before running around to 5 tables.
SELECT ...
FROM
( SELECT orders.pricelist_id AS id
FROM pricelist
JOIN `orders` ON `orders`.`pricelist_id` = `pricelist`.`id`
WHERE `orders`.`free` != 1
AND pricelist.menu_id = 3
ORDER BY `orders`.`id` DESC
LIMIT 10
) AS x
JOIN `pricelist` ON pricelist.id = x.id
JOIN `menu` ON `menu`.`id` = `pricelist`.`menu_id`
JOIN `vendors` ON `vendors`.`id` = `pricelist`.`vendor`
JOIN `orders` ON `orders`.`pricelist_id` = x.`id`
JOIN `users` ON `users`.`id` = `orders`.`user_id`
ORDER BY `orders`.`id` DESC
There is a unknown -- are the tables 1:1 or 1:many or many:1 or many:many? With the 'wrong' answers, you will get more than 10 rows, and the design is flawed.

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 order by the group by in mysql?

I have the code below but i want to arrange it by date_available desc and group it by store_id so that i will only return 1 value.
SELECT
product_to_store.store_id,
product.date_available,
product.product_id,
product_description.name,
product_to_category.category_id,
pcd3.name,
lvl2.parent_id,
pcd2.name,
CASE
WHEN lvl2.parent_id > 0 THEN lvl1.parent_id
ELSE 0
END,
pcd1.name,
product.quantity,
product_type.type
FROM product
inner join product_to_store on (product.product_id=product_to_store.product_id)
inner join product_description on (product.product_id = product_description.product_id)
inner join product_to_category on (product.product_id = product_to_category.product_id)
inner join product_type on (product.product_id = product_type.product_id)
inner join store on (product_to_store.store_id = store.store_id)
inner join product_category as lvl2 on (product_to_category.category_id = lvl2.category_id)
inner join product_category as lvl1 on (lvl1.category_id = lvl2.parent_id)
inner join product_category_description as pcd3 on (pcd3.category_id = lvl2.category_id)
inner join product_category_description as pcd2 on (pcd2.category_id = lvl2.parent_id)
inner join product_category_description as pcd1 on (pcd1.category_id = lvl1.parent_id)
Sample table:
store_id date_available name
1 2017-05-04 T1
1 0000-00-00 T2
2 2017-06-04 T3
3 0000-00-00 T4
2 2017-04-04 T5
3 0000-00-00 T6
Expected result:
store_id date_available name
1 2017-05-04 T1
2 2017-06-04 T3
3 0000-00-00 T4
How can i do that in mysql? Please help me thank you.
Use this as a hint:
-- Create some testing data
IF OBJECT_ID('tempdb..#product') IS NOT NULL DROP TABLE #product
CREATE TABLE #product(
[store_id] int
,[date_available] varchar(50)
,[name] varchar(50)
)
INSERT INTO #product VALUES
(1, '2017-05-04', 'T1')
,(1, '0000-00-00', 'T2')
,(2, '2017-06-04', 'T3')
,(3, '0000-00-00', 'T4')
,(2, '2017-04-04', 'T5')
,(3, '0000-00-00', 'T6')
-- Get result
SELECT p1.[store_id], p1.[date_available], MIN(p2.[name]) AS [name]
FROM (
SELECT [store_id], MAX([date_available]) AS [date_available] FROM #product
GROUP BY [store_id]
) p1
INNER JOIN #product p2
ON p1.[store_id] = p2.[store_id] AND p1.[date_available] = p2.[date_available]
GROUP BY p1.[store_id], p1.[date_available]
-- Destroy the temp table
DROP TABLE #product
Then in your long script, you may rewrite it to something like this:
SELECT product_to_store.store_id,
product.date_available,
product.product_id,
.
.
.
FROM product
INNER JOIN ...
INNER JOIN ...
INNER JOIN (
SELECT p1.[store_id], p1.[date_available], MIN(p2.[name]) AS [name]
FROM (
SELECT [store_id], MAX([date_available]) AS [date_available] FROM #product
GROUP BY [store_id]
) p1
INNER JOIN #product p2
ON p1.[store_id] = p2.[store_id] AND p1.[date_available] = p2.[date_available]
GROUP BY p1.[store_id], p1.[date_available]
) store ON (product_to_store.store_id = store.store_id)
INNER JOIN ...
INNER JOIN ...

MYSQL Update AND SUM

i am try to do somethink like this
UPDATE BA C
INNER JOIN
(
SELECT SUM(`Amount`) AS `LSC`,`To` FROM `Trans`
) A ON C.`UserID` = A.`To`
INNER JOIN
(
SELECT SUM(`Amount`) AS `LSC`,`From` FROM `Trans`
) B ON C.`UserID` = B.`From`
SET `Bal` = A.`LSC` - B.`LSC`;
and this is not workink :\
when i just do 1 inner join and run the query twice once to A and second to be it work but i want to do it in one query ...
UPDATE BA C
INNER JOIN
(
SELECT SUM(`Amount`) AS `LSC`,`To` FROM `Trans`
) A ON C.`UserID` = A.`To`
SET `Bal` = A.`LSC`; // Work