MySQL field 'option' is not working - mysql

MySQL query is
SELECT option as value, name as text from jos_components where parent = 0 and enabled=1 order by name
I have a table jos_components which have field name option. I want to run above query but it gives me an 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 'option as value, name as text from jos_components where parent = 0 and enabled=1' at line 1
What's the problem?

option is a MySQL reserved word. You'll have to surround it with backticks in your query. Try this:
SELECT `option` as value, name as text from jos_components where parent = 0 and enabled=1 order by name

Related

MySQLi error on select query with where field='string'

I just moved to mySQL 8 and have errors on queries that worked in mySQL 5 before.
I can get the query to work without the string matching part, where it just matches an interger field, but it errors when I add the string part.
Query: select * from crew where webvisible = 1 and active = 1 and rank = 'member'
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 '= 'member'' at line 1
I need to query based on the rank field where it is different string values like: member or member applicant, or stowaway, etc.
rank is a reserved word in MySQL 8+. You need to enclose it in backticks i.e.
select * from crew where webvisible = 1 and active = 1 and `rank` = 'member'

when i fetch the data from database it give error on sql command . how can i fixed the code

SELECT (SUM(MAX(identifier)+1) FROM student_ids WHERE state ='state' AND year='year');
SELECT (SUM(MAX(identifier)+1) FROM student_ids WHERE state ='state' AND year='year')
LIMIT 0, 25
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 'FROM student_ids WHERE state ='state' AND year='year')
LIMIT 0, 25' at line 1
how can i fix the error
The SUM function is for taking aggregates of columns, across multiple rows. You don't need to use it to add a scalar value:
SELECT MAX(identifier) + 1 AS max_id
FROM student_ids
WHERE state = 'state' AND year = 'year';
Additionnaly to #tim-biegeleisen, use ` for column names and table names, like this : `year`, `state`, ...
Because some words you uses are reserved. YEAR is reserved word and could return an error if you uses it as column name. SQL thinks you are calling the year function instead of a column named year.
SELECT MAX(`identifier`) + 1 AS `max_id`
FROM `student_ids`
WHERE `state` = 'state'
AND `year` = 'year' ;
Here is documentation about reserved words in mySQL :
https://dev.mysql.com/doc/refman/5.5/en/keywords.html

MySQL SELECT with fields less and greater than values

I am trying to do a select query on MySQL with phpMyAdmin or PHP with PDO.
SELECT 'uid' FROM 'clusters' WHERE 'lat'<='47.21125' AND 'lat'>='39.21125' AND 'lng'<='32.90243' AND 'lng'>='22.90243'
However, phpMyAdmin says:
#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 ''clusters' WHERE 'lat'<='47.21125' AND 'lat'>='39.21125' AND 'lng'<='32.90243' A' at line 1
What is wrong with it?
'' creates a string literal in MySQL, so your query is selecting the literal "uid" from the literal "clusters," which is invalid. Use backtics (or nothing)
SELECT Uid FROM clusters WHERE lat <= 47.21125 AND lat >= 39.21125
AND lng >= 22.90243

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).

codeigniter active record against match error

why this will make an error?
$this->db->where('MATCH (title) AGAINST ("stackoverflow")', NULL, TRUE);
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 'AGAINST
("stackoverflow") LIMIT 4' at line 3
SELECT * FROM (table) WHERE MATCH
(title) AGAINST ("stackoverflow")
LIMIT 4
when change to FALSE,the query will ok,but i want to protect mytable with with backticks
Standard SQL uses single quotes, not double quotes.
MATCH (title) AGAINST ('stackoverflow')
You can test whether this is really the problem with a string as your WHERE clause.
$where = "MATCH (title) AGAINST ('stackoverflow') LIMIT 4";
$this->db->where($where);