Please help - mysql query problems [duplicate] - mysql

This question already has answers here:
How do you use the "WITH" clause in MySQL?
(7 answers)
Closed 6 years ago.
I want to use a SQL Query with the WITH clause an I get a Syntax Error.
I´m using MySQL Version 5.6.28
Here a simple Code example
WITH alias_test AS (SELECT id, title FROM `tips_locations`)
SELECT id, title
FROM alias_test
Here the error I get in my SQL Tool
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 'alias_test AS (SELECT id, title FROM tips_locations) SELECT
id, title FROM ali' at line 1
Can you Help me?

MySQL doesn't support WITH clause or CTE and thus the error. Alternative, you can either use a temporary table or a normal table like
CREATE TEMPORARY TABLE alias_test AS
SELECT id, title FROM `tips_locations`;
SELECT id, title
FROM alias_test;

Related

MySql: Using an alias in IF/IFNULL

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

SQL syntax error in Mysql table [duplicate]

This question already has an answer here:
Syntax error due to using a reserved word as a table or column name in MySQL
(1 answer)
Closed 6 years ago.
I'm trying to run the query
select * from Order;
I m getting the following syntax error in sql fiddle.
Could not figure out the correct syntax.
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 'Order' at line 1
http://sqlfiddle.com/#!9/aee9e/62
Order is a MySQL reserved word. Change your query to use backticks:
select * from `Order`;
Order is a reserved keyword for mysql refer to this link;
Simply change your table name from Order to orders.
Bye!

How to choose a table that has the table name : "group"? [duplicate]

This question already has an answer here:
Syntax error due to using a reserved word as a table or column name in MySQL
(1 answer)
Closed last month.
I have a table. Name of the table is "group"
I run query like this:
SELECT * FROM GROUP
There exist error like this:
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 'group
LIMIT 0, 1000' at line 1
Any solution to solve my problem?
use backquote :
SELECT * FROM `GROUP`
you can also alias your table name for later use in WHERE clause for instance:
SELECT * FROM `GROUP` g WHERE g.id = 1

MySQL Update Query Not Working For Different Columns [duplicate]

This question already has an answer here:
Syntax error due to using a reserved word as a table or column name in MySQL
(1 answer)
Closed 8 years ago.
I've came across strange issue. I know I'm missing something very minor. Can Any One solve the problem plz.
This is my table:
cat_id name desc
1 Cricket Schedule
2 Live Cricket Live Cricket Desc
3 Fixtures
4 Videos
I ran following update query and worked fine.
UPDATE cats
set name='New Fixtures'
WHERE cat_id='3'
But When I Run Following Query, It returns 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 'desc='New Fixtures Desc' WHERE cat_id='3'' at line 1
The Query is :
UPDATE cats
set desc='New Fixtures Desc'
WHERE cat_id='3'
Plz Tell me What I am Missing Here
desc is a reserved keyword, you must escape it with backtick
UPDATE cats
set `desc` = 'New Fixtures Desc'
WHERE cat_id = '3'
MySQL Reserved Keywords List

MySql - Can't sort a varchar column? [duplicate]

This question already has an answer here:
Syntax error due to using a reserved word as a table or column name in MySQL
(1 answer)
Closed 8 years ago.
I'm trying to do a really simple sort on a varchar column, and I get a syntax error. Tested both with PHP and phpmyadmin.
This is the query:
SELECT * FROM cards_available_properties ORDER BY option
this is the 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 'option LIMIT 0, 30' at line 1
OPTION is a reserved keyword. escape it with backtick.
SELECT * FROM cards_available_properties ORDER BY `option`
MySQL Reserved Words