MySQL SELECT with fields less and greater than values - mysql

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

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'

SQL Convert 'mm/dd/yy' string to date format

I have table named templog and I have a date column named 'tdate' which stores a string in the 'mm/dd/yy' format. I tried to convert using the following syntax but I receive an error.
SELECT convert(datetime,tdate,110) from templog
SQL query: Documentation
SELECT convert(datetime,tdate,110) from templog LIMIT 0, 25
MySQL said: Documentation
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'tdate,110) from templog LIMIT 0, 25' at line 1
Any ideas on what I'm doing wrong?
For MariaDB, you want str_to_date():
SELECT str_to_date(tdate, '%m/%d/%y')
FROM templog

Why does this strcmp() syntax give me an error in mysql-5.7?

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.

MySQL date function not accepting sub queries

I am trying to run a sub query within my TIMESTAMP function in MySQL v5.7.11.
SELECT TIMESTAMP(SELECT NOW() AS currentDate);
I get an error saying
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 NOW() AS currentDate)' at line 1
What am I doing wrong here?
You need to add extra pair of brackets inside your subquery.
SELECT TIMESTAMP((SELECT NOW() AS currentDate));

MySQL field 'option' is not working

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