I'm getting an error when I'm using intersect or minus operator in MySQL. Help me with this. What is the alternative I can use instead of these operators?
select * from student_master;
intersect
select * from teacher_student_master;
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 'intersect select * from teacher_student_master' at line 1 0.000 sec
Related
Just using this simple code to create CTE, in the end I want to create more complex code, that's why I am using CTE.
Problem Link
WITH TOTAL_SUBMISSIONS AS(
SELECT * FROM View_Stats)
SELECT * FROM TOTAL_SUBMISSIONS;
I get this Error
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 'TOTAL_SUBMISSIONS AS(
SELECT * FROM View_Stats)
SELECT * FROM TOTAL_SUBMISSIONS' at line 1
Does anyone know how to resolve this issue?
Put the CTE into a subquery:
SELECT TOTAL_SUBMISSIONS.*
FROM (SELECT * FROM View_Stats) AS TOTAL_SUBMISSIONS
I am trying to execute a WINDOW query which is given below:
SELECT
DATE(ts),
oi,
avg(oi) OVER(PARTITION BY DATE(ts) )
FROM bybit_oi_1d_BTCUSD
It gives the following error:
#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 '(PARTITION BY DATE(ts) )
FROM bybit_oi_1d_BTCUSD LIMIT 0, 25' at line 4
No Window functions until MySQL 8+ or MariaDB 10.2
I was trying to test out the strcmp() function with strings
containing random email addresses in this SQL statement:
INSERT IGNORE INTO possible_duplicate_email
-> (human_id, email_address_1, email_address_2, entry_date)
-> VALUES(LAST_INSERT_ID(), 'bobyfischer#mymail.com',
'bobbyfischer#mymail.com')
-> WHERE ABS( STRCMP('bobbyrobin#mymail.com', 'bobyrobin#mymail.com') ) = 1;
Then I got this error message:
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 'WHERE ABS( STRCMP('bobbyrobin#mymail.com',
'bobyrobin#mymail.com') ) = 1' at line 4
I've read the strcmp() documentation from the mysql-5.7 reference manual
and tried a simple SELECT statement to see that strcmp() returned a numeric
value ranging -1, 0, 1, which it did, and I've included the IGNORE option
to the SQL statement so as to proceed through errors. Could someone explain to me why I get this error when running strcmp() in the WHERE clause?
Thanks to Paul T., the answer is:
INSERT statements do not have a WHERE clause, unless doing an INSERT ... SELECT statement, then the SELECT portion of the query can use a WHERE clause.
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).