MySQL - Using * in a conditional statement - mysql

I am attempting to execute the following statement...
SELECT
SUM(CASE WHEN CLG ='A*' THEN 1 END) as A*
From Grades
However, I receive the following error...
"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '*,"
I realise of course that * is used in select statements to select all the rows of a table. However in my case, I want to find the number of records that contain the value of A*. Would anyone be able to point out what I can do to solve this error without having to remove the A* values from my table?

You need to use backticks:
SELECT SUM(CASE WHEN CLG ='A*' THEN 1 END) as `A*`
From Grades;
Actually you could skip CASE:
SELECT SUM(CLG ='A*') as `A*`
From Grades

Your problem is the column alias. Don't use inappropriate characters for column names. Do something like this:
SELECT SUM( CLG = 'A*' ) as A_star
From Grades;
Having to deal with identifiers that use unusual characters is just a pain -- making queries hard to write and to read.

Related

How to use multiple SELECT statement in INSERT statement and also in CONCAT() function

I'm working on a project where i want to insert data from another table and also use select statement in concatenate function but i can't understand ?
INSERT INTO c_order
(oid,cid,servicename,servicetype,servicecategory,price,address,date,status,time)
VALUES
('qw121','121',(select servicename,servicetype,price, from inner_subservice where inssid=1),(select building,city,pincode CONCAT(building,'',city,'',pincode) as fullname from address where cid='121',now(),'ongoing',null);
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'from inner_subservice where inssid=1),(select building,city,pincode CONCAT(build' at line 1
INSERT INTO c_order(oid,cid,servicename,servicetype,servicecategory,price,address,date,status,time)
VALUES("qw121","121",
(select servicename,servicetype,price from inner_subservice where inssid=1),
(select building,city,pincode, CONCAT(building,'',city,'',pincode) as fullname from address where cid='121'),now(),
'ongoing',null);
You were missing some parenthesis and you had some extra comas there. Apart from those this query should work fine.
INSERT INTO c_order(oid,cid,servicename,servicetype,servicecategory,price,address,date,status,time) VALUES("qw121","121",(select servicename,servicetype,servicecategory,price from inner_subservice where inssid=1),(select CONCAT(building,'',city,'',pincode) as fullname from address where cid='121'),now(),'ongoing',null);
Couldn't comment on pr1nc3's answer because i don't have enough rep, but his query needed a small tweak and hopefully this would work.
Instead of selecting building, city and pincode from address you only need to select the concat() result of the respective fields. Also you didn't select servicecategory field.

MySQL: Ordering of columns when using a wildcard with group by has odd behavior

What is the difference between these two MySQL statements?
Works:
select *, count(mycol) c from mytable group by mycol;
Doesn't work:
select count(mycol) c, * from mytable group by mycol;
The first statement works as I'd expect, while the second one gives me a syntax error. Why does the order matter?
I'm having trouble finding an answer from Google, because I'm not entirely sure if I'm asking the question correctly.
Edit:
Here's the sanitized error message. I'm using MySQL Workbench, if that's relevant.
Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '* from mytable group by id' at line 1
Just alias the table and the syntax error will go away.
select count(t.id) c, t.* from mytable t group by id;
See this db fiddle.
It looks like MySQL allows bare (unqualified) * only as immediatly following SELECT. The following query also raises a syntax error :
select 1, * from mytable t;
The documentation prevents against using bare * combined with other items in the SELECT list :
A select list consisting only of a single unqualified * can be used as shorthand to select all columns from all tables.
Use of an unqualified * with other items in the select list may produce a parse error. To avoid this problem, use a qualified tbl_name.* reference.

SQL: Syntax error

I am executing this query in MySql:
SELECT amount
FROM Prices
WHERE (item_id = 1246 AND
('2016-12-26' BETWEEN (effective_date AND COALESCE(end_date, NOW()))))
But for some reason I get a syntax error that I don't see where it is.
the error is:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')) AND(item_id = 1419 AND ('2017-01-14' BETWEEN (effective_date AND COALESCE(end' at line 1
the Price is like this:
Prices
id
item_id
effective_date
end_date
I don't think there should be parentheses between BETWEEN and the first term of that expression. Something like this should work:
SELECT amount
FROM Prices
WHERE item_id = 1246 AND
'2016-12-26' BETWEEN effective_date AND COALESCE(end_date, NOW())
This question is a typo, but maybe this answer would be useful to anyone who wants to know the proper way to use BETWEEN.
The MySQL documentation for BETWEEN doesn't explicitly mention anything about parentheses, but it seems to be implying this based on the examples given.
Based on testing this locally, parentheses around each of the two terms in the BETWEEN expression are OK, e.g.
WHERE '2016-12-26' BETWEEN (effective_date) AND (COALESCE(end_date, NOW()))
However, putting parenthesis around the entire clause generates an error, which is what you were doing:
WHERE '2016-12-26' BETWEEN (effective_date AND COALESCE(end_date, NOW()))

How to put a CASE statement in the GROUP BY clause

I've got a table with one column that is a comma separated list of possible values. I'd like to query, grouped by each individual possible value.
As a test, I've written this query:
SELECT
`Please_identify_which_of_the_following_classroom_hardware_you_c2`,
count(`_How_would_you_rate_your_overall_skill_in_using_educational_tec1`) as count,
`_How_would_you_rate_your_overall_skill_in_using_educational_tec1`
FROM
`data_Copy_of_Faculty_survey_on_technology_in_the_classroom_Respo`
GROUP BY
`_How_would_you_rate_your_overall_skill_in_using_educational_tec1`,
CASE
WHEN `Please_identify_which_of_the_following_classroom_hardware_you_c2` LIKE '%Elmo%' THEN 'Elmo'
END
(Please excuse the column names, they're auto-generated)
I know the CASE statement isn't incredibly useful at this point, but I'm just trying to get the query to run. I'm getting an error:
ERROR 1064 (42000): You have an error in your SQL syntax; check the
manual that corresponds to your MySQL server version for the right
syntax to use near 'THEN 'Elmo' END' at line 10
For the life of me I can't find what's wrong with the query. Any insight would be appreciated.
EDIT: I've tried with single and double quotes - same problem regardless of the quote used.
UPDATED: As Mark has pointed out, even if I get this query to parse, the results won't be what I'm looking for. I'm still curious why this doesn't parse, but the query is not the solution to my initial problem.
The reason you're seeing issues is that your GROUP BY attributes didn't align with the SELECT attributes.
As the MySql docs put it:
"SQL92 and earlier does not permit queries for which
the select list, HAVING condition, or ORDER BY list refer
to nonaggregated columns that are neither named in the GROUP BY
clause nor are functionally dependent on (uniquely determined by)
GROUP BY columns"
In other words, since the ...c2 attribute was not "functionally dependent on" your CASE ... END attribute, there was a mismatch between SELECT and GROUP BY, and thus an error.
One way to mitigate the error (and possibly make the query more readable), is to do the CASE once and then do the aggregates on the resulting relation.
SELECT c2, tec1, COUNT(tec1)
FROM
(SELECT
CASE
WHEN `Please_identify_which_of_the_following_classroom_hardware_you_c2` LIKE '%Elmo%'
THEN 'Elmo'
ELSE
`Please_identify_which_of_the_following_classroom_hardware_you_c2`
END AS c2,
`_How_would_you_rate_your_overall_skill_in_using_educational_tec1`) AS tec1
FROM
`data_Copy_of_Faculty_survey_on_technology_in_the_classroom_Respo`) t
GROUP BY c2, tec1
Try This:
SELECT
CASE
WHEN `Please_identify_which_of_the_following_classroom_hardware_you_c2` LIKE '%Elmo%' THEN 'Elmo'
ELSE `Please_identify_which_of_the_following_classroom_hardware_you_c2`
END AS `Please_identify_which_of_the_following_classroom_hardware_you_c2`,
count(`_How_would_you_rate_your_overall_skill_in_using_educational_tec1`) as count,
`_How_would_you_rate_your_overall_skill_in_using_educational_tec1`
FROM
`data_Copy_of_Faculty_survey_on_technology_in_the_classroom_Respo`
GROUP BY
`_How_would_you_rate_your_overall_skill_in_using_educational_tec1`,
CASE
WHEN `Please_identify_which_of_the_following_classroom_hardware_you_c2` LIKE '%Elmo%' THEN 'Elmo'
ELSE `Please_identify_which_of_the_following_classroom_hardware_you_c2`
END

does the existence of an asterisk in a select exclude other columns?

This question is all about laziness... I'd like to do something like this:
select some_func(some_col), * from my_table
So that I don't have to do this:
select some_func(some_col), col_1, col_2... col_ad_infinitum from my_table
Is there any way to make the first query work? This is the error I get when I run it:
ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '* from my_table' at line 1
Do you mean that in MySQL your first query:
SELECT some_func(some_col), *
FROM my_table
produces this error?:
Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '*' at line 1
You can change your code into (this results in no errors!):
SELECT *, some_func(some_col)
FROM my_table
or into this, if you want to have the calculated columns first:
SELECT some_func(some_col), t.*
FROM my_table AS t
Unfortunately, mysql only supports the asterisk at the start of the column list (unlike every other DB I am familiar with)
(Edited: start not end - oops!)
Change the order of your select params:
select *,some_func(some_col) from my_table
Anyway, as the Zen of Python says: "Explicit is better than implicit". Always try to write the fields you're selecting, and if it's posible try to put the table they're from too, you can use an alias. Your future YOU will thank you.
select t.some_col from my_table t
When I do that with PostgreSQL, I get the column(s) I specify followed by all the other columns (possibly repeating the column(s) I specified).