I'm new to mysql so please be kind. I'm getting the following error for my script and im not sure whats wrong with it.
SELECT uoid
FROM mint
WHERE mint_id='6' and userid='3836'
INTERSECT
SELECT id as uoid
FROM cats
WHERE category='Health, Fitness'
ORDER BY 1;
gives
#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 'INTERSECT SELECT id as uoid FROM cats WHERE category='Health, Fitness'' at line 4
MySQL does not have an INTERSECT keyword. See this question and this one for suggestions about how to achieve what you're after.
INTERSECT is not supported in MySQL. You need to restructure your query somehow. You might be able to use a subquery if you version of MySQL supports subqueries.
This is because intersect only work on same table, not on different tables as you are trying.
See this
In place of intersect use join statement syntax.
And INTERSECT does exist in sql
The INTERSECT operator has become available for MySQL since version 8.0.31, released in 10 November 2022. You can now do the following:
SELECT uoid FROM mint WHERE mint_id='6' and userid='3836'
INTERSECT
SELECT id as uoid FROM cats WHERE category='Health, Fitness'
ORDER BY 1;
Related
I have a database relation fake_apps with prices of fake apps.Now I'm trying to calculate the rounded value of avg of all prices upto 2 decimal places.
I can't understand why this query doesn't work.
SELECT ROUND(SELECT AVG(prices) FROM fake_apps,2) AS round_val;
It gives a syntactical error as follows on MySQL 8.0.18:
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.
You need two sets of parentheses, one for ROUND() and one for the subquery:
SELECT ROUND( (SELECT AVG(prices) FROM fake_apps), 2) AS round_val;
Specifically, subqueries in SQL require their own sets of parentheses.
Here are two alternative methods of writing the query.
Put the ROUND() in the subquery:
SELECT (SELECT ROUND(AVG(prices), 2) FROM fake_apps) AS round_val;
Dispense with the subquery altogether:
SELECT ROUND(AVG(prices), 2) AS round_val
FROM fake_apps ;
The statement is like SELECT * FROM db.table group by id desc;
Would raise an error like
15:02:24 SELECT * FROM db.table group by id
desc LIMIT 0, 10 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 'desc LIMIT 0, 10' at line 1 0.00014
sec
on MySQL 8.0.13 in Ubuntu 18.04 Desktop 64bit
which would be fine on MySQL 5.7 in Windows or CentOS or Ubuntu.
I know basically, the select statement is like.
SELECT statement... [WHERE condition | GROUP BY `field_name(s)` HAVING condition] ORDER BY `field_name(s)` [ASC | DESC];
So is this 5.7's problem not to issue the error?
Or something more complicated on SQL standard?
I have the same issue, so for MySQL 8, I used the sql like that:
SELECT * FROM db.table
group by id
order by id desc
Taking from #P.Salmon's comment for the question.
If you look up the select statement in the manual
http://dev.mysql.com/doc/refman/5.7/en/select.html you will see
that up to 5.7 asc|desc are optional modifiers to the group by
statement which are no longer present from 8.0.and if you look at the
upgrade documentation
https://dev.mysql.com/doc/refman/8.0/en/upgrading-from-previous-series.html#upgrade-sql-changes
This deprecation is documented.
Since this situation, #Linda Li's answer could be a good option.
This query makes no sense:
SELECT *
FROM db.table
GROUP BY id DESC;
You are doing an aggregation query. So (presumably), the table has multiple rows per id. Those are condensed down to one row. What values should be used for the other columns? It is sad that MySQL ever supported this syntax. So a welcome change is that ONLY_FULL_GROUP_BY is now the default.
A small hint is that using an aggregation query with no aggregation functions is suspicious.
Perhaps you want:
select id, min(col1), min(col2), . . .
from t
group by id;
Or more likely, you want a particular row, such as the "earliest" or "most recent", something like:
select t.*
from t
where t.createdAt = (select min(t2.createdAt) from t t2 where t2.id = t.id);
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.
I found this post:
Using an Alias in SQL Calculations
which suggests you can use an alias in calculations by using
(select alias)
like:
SELECT 10 AS my_num,
(SELECT my_num) * 5 AS another_number
FROM table
This works fine. But now I want to use an alias in an if. So I thought it might work the same way. So I tried:
SELECT 10 AS my_num,
IFNULL(otherfield, (SELECT my_num)) AS another_number
FROM table
which doesn't work at all, telling me
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 '(SELECT my_num)) AS another_number
Is there any way to make this work in MySql?
No, use the entire expression directly like below unless you are accessing it in a outer query.
IFNULL(otherfield, 10) AS another_number
In your case it should be
SELECT 10 AS my_num,
IFNULL(otherfield, 10) AS another_number
FROM table
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).