I want to execute a if statement which is like this
select IF ( quantity_wanted!=3,(select name from grocerywanted),Null) AS message from grocerywanted;
I want here to check the quantity_wanted column if it contains a value except 3 run the select statement
Your query is using same table name not as sub query. I think you should use 'name' in replace of '(select name from grocerywanted)'. You will get your desired output.
select IF ( quantity_wanted!=3,name,Null) AS message from grocerywanted;
In my SQL table I have "country" and "we200326" columns. Column "we200326" contains only "1" or "NULL" entries.
I'm trying to get a total of all "1"s in column "we200326" and a total by country. I have written the following statement but it gives an error but I don't know what I did wrong (I'm very new at this):
SELECT country, we200326,
(SUM(we200326) OVER () AS Total)
(SUM(we200326) OVER (PARTITION BY country) AS CountryTotal)
FROM table_name
ORDER BY CountryTotal, Country;
The error I get is this:
MySQL said: Documentation
#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 'OVER () AS Total)
(SUM(we200326) OVER (PARTITION BY country) AS CountryTotal)
' at line 2
I have searched for similar errors and found several (each time was a simple syntax error like a space or comma or so) I tried several versions but could not resolve my problem when following those instructions. Any help would be appreciated.
Window functions are available in MySQL 8.0 only.
In earlier versions, one option is to use subqueries:
select
country,
wewe200326,
(select sum(we200326) from table_name) total,
(select sum(we200326) from table_name t1 where t1.country = t.country) country_total
from table_name t
order by country_total, country
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.
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.
The following query works:
SELECT DISTINCT `number`
FROM `employee`
WHERE `number` IN
(SELECT `department_manager`
FROM `department`)
UNION
(SELECT DISTINCT `manager`
FROM `employee`
WHERE `manager` IS NOT NULL)
But as soon as I wrap the query with parentheses it doesn't work anymore:
(SELECT DISTINCT `number`
FROM `employee`
WHERE `number` IN
(SELECT `department_manager`
FROM `department`)
UNION
(SELECT DISTINCT `manager`
FROM `employee`
WHERE `manager` IS NOT NULL))
Causing syntax 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 'UNION
Wrapping other select queries in parentheses doesn't cause problems, this works for example:
(SELECT DISTINCT number FROM johnson.employee);
What is the difference between these?
This has to do with the way MySQL implemented its SQL grammar.
A <query> can be:
SELECT ... [ UNION <query>]
or
( SELECT ... ) [ UNION <query> ]
But apparently not
( SELECT ... UNION <query> )
Read sql/sql_yacc.yy in the MySQL source code if you want the details.
As a workaround, you can do this:
SELECT * FROM (SELECT ... UNION SELECT ... ) AS t;
Parentheses around queries are subqueries -- think of this almost like a subroutine, where the inner query is executed first, then its result is evaluated in the context of the outer query. In your second example, you are defining a subquery where no outer query exists, hence a syntax error.
You might be able to transform the second example into valid sql simply by putting a SELECT before the first parenthesis and 'FROM DUMMY' after the closing parenthesis; try it.