So my mysql query's been loading for 25sec every time. I split query and found out that it works perfectly without one of WHERE conditions. Condition causing problem is :
eshop_products.id IN
(SELECT product-id
FROM eshop_productCombinations
WHERE eshop_productCombinations.recomended = 1
GROUP BY product-id)
Without this condition query took 0.019 sec to load. BUT when I execute this select separately, it takes only 0.026 sec to load:
SELECT product-id
FROM eshop_productCombinations
WHERE eshop_productCombinations.recomended = 1
GROUP BY product-id
Does anyone have any idea what's wrong with my main query? Thank you.
Here's full query (although I don't think it'd be useful for anybody):
SELECT
CAST(
SUBSTRING_INDEX(
GROUP_CONCAT(
price_with_vat ORDER BY IF(eshop_products_cache.`stock` > 0, 1, 0) DESC,
IF(
eshop_products.`type_default_price`=2,eshop_products_cache.`price_with_vat`,
if(
eshop_products.`type_default_price`=0,eshop_products_cache.`default`,null
)
) DESC,
IF(eshop_products.`type_default_price`=1,eshop_products_cache.`price`, null) ASC
),
",
",
1
) AS DECIMAL(10,2)
) AS `price_with_vat`,
SUBSTRING_INDEX(
GROUP_CONCAT(
eshop_products_cache.combination_id ORDER BY IF(eshop_products_cache.`stock` > 0, 1, 0) DESC,
IF(
eshop_products.`type_default_price`=2,
eshop_products_cache.`price_with_vat`,
if(
eshop_products.`type_default_price`=0,
eshop_products_cache.`default`,
null
)
) DESC,
IF(eshop_products.`type_default_price`=1,eshop_products_cache.`price`, null)
ASC
),
",
",
1
) AS `combination_id`,
if( eshop_products.id in ('5993', '6144', '6663', '5120', '5376', '5632', '5888', '6400', '6656', '5121', '5377', '5633'), 1, 0) AS new
FROM `eshop_products` LEFT JOIN `eshop_products_cache` ON eshop_products_cache.product_id=eshop_products.`id` WHERE
(
(
(
eshop_products.stockType = 2 AND eshop_products_cache.stock > 0
)
OR eshop_products.stockType <> 2
)
)
AND
(
price_with_vat > 0
)
AND
(
eshop_products.recomended = 1
OR
eshop_products.id IN (
SELECT `product-id` FROM eshop_productCombinations WHERE eshop_productCombinations.recomended = 1 GROUP BY `product-id`
)
)
AND
(
eshop_products.active = '1'
)
AND (dateStartPublish <= NOW() OR dateStartPublish IS NULL)
AND (dateStopPublish >= NOW() OR dateStopPublish IS NULL)
GROUP BY `eshop_products`.`id`, `eshop_products_cache`.`product_id` ORDER BY RAND() ASC LIMIT 5
Suggested by Anthony , subquery has to be replaced with code below:
EXISTS (
SELECT 1 FROM eshop_productCombinations
WHERE eshop_productCombinations.recomended = 1
AND product-id = eshop_products.id )
Related
I have written a complex query in MySQL to return a result.
it works perfectly
Until .....
the subquery returns no result
how to write an if or IF null then substitute in date '2020-06-03'
any help greatly appreciated
SELECT
*
FROM
trades
WHERE
stock_code = 'IHVV'
AND acc_id = '4'
AND tx_date >
(SELECT tx_date
FROM
( SELECT *, ( #sum_units := #sum_units + units ) AS sum_units
FROM
trades
JOIN ( SELECT #sum_units := 0 ) params
WHERE
stock_code = 'IHVV'
AND acc_id = '4'
AND tx_date <= '2021-06-30'
AND ( transfer_date IS NULL OR transfer_date <= '2021-06-30' )
ORDER BY
tx_date ASC,
units ASC
) AS query1
WHERE
tx_date < DATE_SUB( '2021-06-30', INTERVAL 1 YEAR )
AND sum_units = 0
ORDER BY
tx_date DESC
LIMIT 1
)
AND tx_date <= '2021-06-30'
AND ( transfer_date IS NULL OR transfer_date <= '2021-06-30' )
ORDER BY
tx_date ASC,
units ASC
clarification
I have written a main query and in one of the where clauses I am using a subquery and this subquery works well, until this subquery does not return a result and my main query stops working, so I would like to on no result in this subquery substitute a value on no result so the main query can function normally
example needed
select * from table where date > (ifnull(subquery, "2002-01-01"))
I get
incorrect parameter count in the call to native function "IFNULL'
AND THE ANSWER IS: ---> :)
thanks guys for all your suggestions
much appreciated
SELECT *
FROM trades
WHERE stock_code = 'IHVV'
AND acc_id = '4'
AND tx_date >
#last 0 date out of 1 year perhaps change the date range by from date and to date in the interval 1 year area
(SELECT COALESCE(
(SELECT tx_date
FROM (SELECT *, (#sum_units := #sum_units + units) AS sum_units
FROM trades
JOIN ( SELECT #sum_units := 0 ) params
WHERE stock_code = 'ANZ'
AND acc_id = '4'
AND tx_date <= '2022-06-30'
AND (transfer_date IS NULL OR transfer_date <= '2022-06-30' ))
ORDER BY tx_date ASC, units ASC) as query1
WHERE tx_date < DATE_SUB('2022-06-30',INTERVAL 1 YEAR)
AND sum_units = 0
ORDER BY tx_date DESC
LIMIT 1), 'TODATE')
AND tx_date <= '2022-06-30'
AND (transfer_date IS NULL OR transfer_date <= '2022-06-30' )
ORDER BY tx_date ASC, units ASC
I am trying to get this query to work in JPA
It works perfectly in mysql:
select * from order_header join order_detail where (order_header.buying_partner_id = 1 or order_header.buying_partner_id = 3) AND ((order_detail.warehouse_id = 6) or (order_header.warehouse_id = 6 and order_detail.warehouse_id is null ) ) and (order_detail.tracking_number is null or order_detail.tracking_number = '') and order_detail.canceled != 1 and (order_header.order_num is NOT NULL and order_header.order_num != '') and order_header.timestamp > '2017-01-24 08:33:00.096' group by order_header.order_num order by order_header.comment DESC;
Results are returned.
JPA Query:
Select h FROM OrderHeader h JOIN h.orderDetails d WHERE ( h.buyingPartner.id = 1 or h.buyingPartner.id = 3 ) AND ( (d.warehouse.id =6) OR ( h.warehouse.id =6 AND d.warehouse.id IS null ) ) AND (d.trackingNumber IS null or d.trackingNumber = '') AND d.canceled != 1 AND (h.orderNum IS NOT null AND h.orderNum != '') and h.timestamp > '2017-01-24 09:32:39.865' GROUP BY h.orderNum ORDER BY h.comment DESC
It returns nothing. No errors. Just returns nothing.
These are the exact same statements.
There must be something wrong with my JPA statement.
Select h FROM OrderHeader h JOIN h.orderDetails d WHERE ( h.buyingPartner.id = 1 or h.buyingPartner.id = 3 ) AND ( (d.warehouse.id =6) OR ( h.warehouse.id =6 AND d.warehouse.id IS null ) ) AND (d.trackingNumber IS null or d.trackingNumber = '') AND d.canceled != 1 AND (h.orderNum IS NOT null AND h.orderNum != '') and h.timestamp > '2017-01-24 09:32:39.865' GROUP BY h.orderNum ORDER BY h.comment DESC
JPA Query uses the JOIN condition when you created the relationship between entities.
For raw SQL query you have not provided that ON condition which will be included default in JPA Query.
That's the only difference I see in it.
I have a scenario where i have table app_users in which i am storing multiple types of users , and each user have different column for phone number , and Names , i am trying to get phone number and name of specific type of user , it's hard to understand this way , but see the working example Query below :
SET #sk := (SELECT app_users.skeeper_phone as sphone FROM app_users WHERE app_users. app_user_type = 'SKEEPER' LIMIT 1);
SELECT
acs.id as complainId,
IF(
(#sk) > 0,
#sk,
''
) as sk_phone,
(
SELECT
IF(
auu.institute_phone IS NULL,
auu.department_phone,
auu.institute_phone
) as cphone
FROM app_users as auu
WHERE auu.id = acs.app_customer_id
LIMIT 1
) as cphone,
(
SELECT auuu.other_user_phone as ephone
FROM app_users as auuu,app_admin_assign_eng as aase
WHERE aase.app_complain_service_id = acs.id AND auuu.id = aase.engineer_id
LIMIT 1
) as ephone,
(
SELECT auuu.fullname as ename
FROM app_users as auuu,app_admin_assign_eng as aase
WHERE aase.app_complain_service_id = acs.id AND auuu.id = aase.engineer_id
LIMIT 1
) as ename,
(
SELECT auuu.other_user_phone as ephone
FROM app_users as auuu,app_admin_assign_eng as aase
WHERE aase.app_complain_service_id = acs.id AND auuu.id = aase.admin_id
LIMIT 1
) as aphone
FROM app_complain_services as acs,app_users as au
WHERE
acs.id = 5
GROUP BY acs.id
Above query is working alright , but it is not optimized , what could be optimization and other ways to solve similar problem?
what i need
i need to sort the integer numbers in mysql.
unsubscribe tinyint(6)
1, 1, 4, 5,1,1,...etc
sql query
SELECT e0_.metadata AS metadata0, u1_.unsubscribe AS unsubscribe1, u1_.id AS id2, u1_.linkedin_id AS linkedin_id3, u1_.facebook_id AS facebook_id4, u1_.linkedin_profile AS linkedin_profile5, u1_.profile AS profile6,
CASE WHEN u1_.linkedin_id <> ''
OR u1_.facebook_id <> ''
THEN 1
ELSE 0
END AS sclr7,
CASE WHEN e0_.edition =23932
THEN 'current'
ELSE 'past'
END AS sclr8
FROM event_visitor e0_, user u1_
WHERE e0_.user = u1_.id
AND e0_.event =25162
AND e0_.published =1
AND e0_.showme =1
GROUP BY e0_.user
ORDER BY sclr7 DESC , e0_.id DESC , LENGTH( u1_.unsubscribe )
LIMIT 30
solution i tried.
length
cast
order by col *1 ,col
but no solution is working in tiny int datatype.
problem
there is also negative number like -5.
o/p should be
all 1 should be at top .
finally after googling a lot
ORDER BY CAST(est_dily_pple as SIGNED INTEGER) ASC
ORDER BY CAST( u1_.unsubscribe AS SIGNED INTEGER ) ASC , sclr7 DESC , e0_.id DESC
but problem o/p -5,1,1,..an so on .
Solution i came up:
ORDER BY SIGN( CAST( u1_.unsubscribe AS SIGNED INTEGER ) ) ASC , ABS( CAST( u1_.unsubscribe AS SIGNED INTEGER ) ) , sclr7 DESC , e0_.id DESC .
Refrence
http://stackoverflow.com/questions/11282571/specific-order-by-in-mysql-statement
i need to implement the abs function doctrine
dql code
$from = 'Entities\EventVisitor visitor,Entities\User user';
$qb = $this->em->createQueryBuilder();
$qb->select("visitor.metadata,ABS( u1_.unsubscribe )as unsubscribe)
->add('from',$from)
->where('visitor.user=user.id')
->groupBy('visitor.user')
->orderBy('unsubscribe','asc')
error 500 internal error.
I need to gather sums using conditional statements as well as DISTINCT values
with a multiple GROUP BY. The example below is a simplified version of a much much more complex query.
Because the real query is very large, I need to avoid having to drastically re-write the query.
DATA
Contracts
id advertiser_id status
1 1 1
2 2 1
3 3 2
4 1 1
A Query that's close
SELECT
COUNT( DISTINCT advertiser_id ) AS advertiser_qty,
COUNT( DISTINCT id ) AS contract_qty,
SUM( IF( status = 1, 1, 0 ) ) AS current_qty,
SUM( IF( status = 2, 1, 0 ) ) AS expired_qty,
SUM( IF( status = 3, 1, 0 ) ) AS other_qty
FROM (
SELECT * FROM `contracts`
GROUP BY advertiser_id, id
) AS temp
Currently Returns
advertiser_qty contract_qty current_qty expired_qty other_qty
3 4 3 1 0
Needs to Return
advertiser_qty contract_qty current_qty expired_qty other_qty
3 4 2 1 0
Where current_qty is 2 which is the sum of records with status = 1 for only DISTINCT advertiser_ids and each sum function will need the same fix.
I hope someone has a simple solution that can plug into the SUM functions.
-Thanks!!
try this
SELECT
COUNT( DISTINCT advertiser_id ) AS advertiser_qty,
COUNT( DISTINCT id ) AS contract_qty,
(select count(distinct advertiser_id) from contracts where status =1
) AS current_qty,
SUM( IF( status = 2, 1, 0 ) ) AS expired_qty,
SUM( IF( status = 3, 1, 0 ) ) AS other_qty
FROM (
SELECT * FROM `contracts`
GROUP BY advertiser_id, id
) AS temp
DEMO HERE
EDIT:
you may look for this without subselect.
SELECT COUNT(DISTINCT advertiser_id) AS advertiser_qty,
COUNT(DISTINCT id) AS contract_qty,
COUNT(DISTINCT advertiser_id , status = 1) AS current_qty,
SUM(IF(status = 2, 1, 0)) AS expired_qty,
SUM(IF(status = 3, 1, 0)) AS other_qty
FROM (SELECT *
FROM `contracts`
GROUP BY advertiser_id, id) AS temp
DEMO HERE