mysql conditional statement? - mysql

i am facing a little problem in an sql query,
here is the query
SELECT events.event_id,
( SELECT favorite_events.event_id,
(CASE favorite_events.event_id WHEN NULL THEN 0 ELSE 1 END)
FROM favorite_events
WHERE favorite_events.event_id = events.event_id
) AS is_favorite
FROM events
WHERE start_date = 2013-07-16
it is giving me "Operand should contain 1 column(s)
" error, please help!

Use a JOIN, not a correlated subquery.
SELECT e.event_id,
f.event_id IS NOT NULL AS is_favorite
FROM events e
LEFT JOIN favorite_events f
ON f.event_id = e.event_id
WHERE e.start_date = '2013-07-16'

In the subquery you are selecting 2 columns. You can't rename 2 colums as is_favorite.
I assume, by looking at the query, you probably don't need the field favorite_events.event_id. Just remove that part.

Related

MySQL select in select query

can somebody help me solving and explaining how i can get the following done? :
In my MySQL query i want to select all entries, where the forwarded_to_cordroom value is 0, and in the next row i want to have all where the value is 1, basically i could create 2 identical queries, where the only difference would be the WHERE clause (where forwarded_to_cordroom = 1 , where forwarded_to_cordroom = 0) , and i thought about doing this in one query, but getting the following error with what ive tried:
SELECT
COUNT(DISTINCT o.order_id) as count,
(SELECT o.forwarded_to_cordroom WHERE o.forwarded_to_cordroom = 1)
FROM
`orders_articles` o
LEFT JOIN orders oo ON
o.order_id = oo.order_id
WHERE
(
oo.finished_order_date IS NULL OR oo.finished_order_date >= '2021-09-27'
) AND oo.order_date <= '2021-09-27'
Results in :
#1140 - In aggregated query without GROUP BY, expression #2 of SELECT list contains nonaggregated column 'o.forwarded_to_cordroom'; this is
incompatible with sql_mode=only_full_group_by
I have also tried changing the subselect in various ways (with and without joins etc.) but without success, always the same error.
I'd prefer not turning this mode off, I think that would not be the purpose and that I can fix my query with some help.
Best Regards
Use conditional aggregation:
SELECT COUNT(DISTINCT o.order_id) AS count,
COUNT(CASE WHEN o.forwarded_to_cordroom = 1 THEN 1 END) AS count_1,
COUNT(CASE WHEN o.forwarded_to_cordroom = 0 THEN 1 END) AS count_0
FROM orders_articles AS o
LEFT JOIN orders AS oo ON o.order_id = oo.order_id
WHERE ...

MySQL Count as {name} and WHERE {name} = X, Unknown column

I am trying to filter results based on the name assigned on count() and get this:
Unknown column 'total_submissions' in 'where clause'
SELECT SQL_CALC_FOUND_ROWS patient.*,count(patient_data.id) as total_submissions
FROM patient
LEFT JOIN patient_data ON (patient_data.patient_id = patient.id AND patient_data.date_finished IS NOT NULL)
WHERE patient.doc_id = 2 AND total_submissions = 5
GROUP BY patient.id
ORDER BY patient.id DESC
LIMIT 0,40
After more research I did find out about not being able to use a column alias in the WHERE but I am unsure how to execute this query then. I assume it's possible but how would I be able to filter the results based on the count() calculation of the query?
total_submissions is a column alias and the result of an aggregation function. You need to do that check in a havingclause:
SELECT SQL_CALC_FOUND_ROWS p.*, count(pd.id) as total_submissions
FROM patient p LEFT JOIN
patient_data pd
ON pd.patient_id = p.id AND pd.date_finished IS NOT NULL
WHERE p.doc_id = 2
GROUP BY p.id
HAVING total_submissions = 5
ORDER BY p.id DESC
LIMIT 0, 40;
Notes:
Table aliases make the query easier to write and to read.
The condition on doc_id should still be in the WHERE clause.
You can't use column alias in where clause because the precedence in sql evaluation don't let the db engine know the alias name when evaluate the where clause
First is evaluated the FROM clase then the WHERE clause and after the SELECT cluase ..
In your case you have an aggregation function related to yu alias and this can be evaluated only after the group by is performed, pratically at the end of query process
for this reason there is a proper filter based on HAVING that work on the result of the aggreated query
SELECT SQL_CALC_FOUND_ROWS patient.*, count(patient_data.id) as total_submissions
FROM patient
LEFT JOIN patient_data ON (patient_data.patient_id = patient.id AND patient_data.date_finished IS NOT NULL)
WHERE patient.doc_id = 2
GROUP BY patient.id
HAVING total_submissions = 0
ORDER BY patient.id DESC
LIMIT 0,40

MYSQL left join (if id is in other table then 1 else 0)

i have a table EVENT_COMMENTS and EVENT_COMMENTS_LIKES, and everything works fine except that i want to check if i liked that comment already. So i do a left join with the likes table (has foreign keys (comment_id and user_id)) and if that user_id is my own id i want to output = true.
But it is not working at all. I mean i get the list with l.User_ID of all IDs (which i count for the amount of likes) but i cannot check if my id is in there. 54 is in this case my id.
SELECT c.Comment_ID,c.Event_ID,c.User_ID,count(l.User_ID) as amount_likes,
(case l.User_ID when 54 THEN 'true' ELSE 'false' END) as is_liked
FROM EVENT_COMMENTS c
left join USERINFO u on u.USERID=c.User_ID
left join EVENT_COMMENTS_LIKES l on l.Comment_ID = c.Comment_ID
WHERE Event_ID=121514
GROUP BY c.Comment_ID
ORDER BY updated_time DESC
The bold one is the tricky one.
This is the error: Expression #10 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'markusp.l.User_ID' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
Appreciate your help :)
Try including all of your non-aggregate columns in your GROUP BY. In your example they would be: c.Comment_ID, c.Event_ID, c.User_ID
If every user can like a comnent only once, thenuse condtional counting in place of the (case l.User_ID when 54 THEN 'true' ELSE 'false' END) as is_liked expression:
sum(l.User_ID = 54) as is_liked
The l.User_ID = 54 evaluates as true or false (1 or 0), which is summed up per comment.
Since you use the aggregate function sum(), you will not get the error message shown in the question.
As Shadow says, the error message contains the solution. Try unsetting the sql_mode
SET GLOBAL sql_mode=(SELECT REPLACE(##sql_mode,'ONLY_FULL_GROUP_BY',''));
And re-run the query.

Need to select sum of the same field in SQL with diffrent Conditions

I have two tables used to select sum of the same field with different conditions now tried the query is follows But result showing same in the two fields
SELECT sum( s.message_count) as total,(case when s.dlr_status = 'DELIVRD' then sum( s.message_count) end ) as delivered
FROM `sent` s join `track` t on t.track_id = s.track_id group by t.sent_type
Any help would be appreciable
Thank you
Move the sum outside of the case..when projection, and for completeness, return zero in the 'else'. You'll also need the sent_type column in the select in order to group by it.
SELECT
t.sent_type,
sum(s.message_count) as total,
sum(case when s.dlr_status = 'DELIVRD' then s.message_count else 0 end) as delivered
FROM `sent` s join `track` t on t.track_id = s.track_id
group by t.sent_type;

optimize Mysql: get latest status of the sale

In the following query, I show the latest status of the sale (by stage, in this case the number 3). The query is based on a subquery in the status history of the sale:
SELECT v.id_sale,
IFNULL((
SELECT (CASE WHEN IFNULL( vec.description, '' ) = ''
THEN ve.name
ELSE vec.description
END)
FROM t_record veh
INNER JOIN t_state_campaign vec ON vec.id_state_campaign = veh.id_state_campaign
INNER JOIN t_state ve ON ve.id_state = vec.id_state
WHERE veh.id_sale = v.id_sale
AND vec.id_stage = 3
ORDER BY veh.id_record DESC
LIMIT 1
), 'x') sale_state_3
FROM t_sale v
INNER JOIN t_quarters sd ON v.id_quarters = sd.id_quarters
WHERE 1 =1
AND v.flag =1
AND v.id_quarters =4
AND EXISTS (
SELECT '1'
FROM t_record
WHERE id_sale = v.id_sale
LIMIT 1
)
the query delay 0.0057seg and show 1011 records.
Because I have to filter the sales by the name of the state as it would have to repeat the subquery in a where clause, I have decided to change the same query using joins. In this case, I'm using the MAX function to obtain the latest status:
SELECT
v.id_sale,
IFNULL(veh3.State3,'x') AS sale_state_3
FROM t_sale v
INNER JOIN t_quarters sd ON v.id_quarters = sd.id_quarters
LEFT JOIN (
SELECT veh.id_sale,
(CASE WHEN IFNULL(vec.description,'') = ''
THEN ve.name
ELSE vec.description END) AS State3
FROM t_record veh
INNER JOIN (
SELECT id_sale, MAX(id_record) AS max_rating
FROM(
SELECT veh.id_sale, id_record
FROM t_record veh
INNER JOIN t_state_campaign vec ON vec.id_state_campaign = veh.id_state_campaign AND vec.id_stage = 3
) m
GROUP BY id_sale
) x ON x.max_rating = veh.id_record
INNER JOIN t_state_campaign vec ON vec.id_state_campaign = veh.id_state_campaign
INNER JOIN t_state ve ON ve.id_state = vec.id_state
) veh3 ON veh3.id_sale = v.id_sale
WHERE v.flag = 1
AND v.id_quarters = 4
This query shows the same results (1011). But the problem is it takes 0.0753 sec
Reviewing the possibilities I have found the factor that makes the difference in the speed of the query:
AND EXISTS (
SELECT '1'
FROM t_record
WHERE id_sale = v.id_sale
LIMIT 1
)
If I remove this clause, both queries the same time delay... Why it works better? Is there any way to use this clause in the joins? I hope your help.
EDIT
I will show the results of EXPLAIN for each query respectively:
q1:
q2:
Interesting, so that little statement basically determines if there is a match between t_record.id_sale and t_sale.id_sale.
Why is this making your query run faster? Because Where statements applied prior to subSelects in the select statement, so if there is no record to go with the sale, then it doesn't bother processing the subSelect. Which is netting you some time. So that's why it works better.
Is it going to work in your join syntax? I don't really know without having your tables to test against but you can always just apply it to the end and find out. Add the keyword EXPLAIN to the beginning of your query and you will get a plan of execution which will help you optimize things. Probably the best way to get better results in your join syntax is to add some indexes to your tables.
But I ask you, is this even necessary? You have a query returning in <8 hundredths of a second. Unless this query is getting ran thousands of times an hour, this is not really taxing your DB at all and your time is probably better spent making improvements elsewhere in your application.