MySQL - how to match JSON string in where condition? - php

I am using MySQL 5.6 , one of my table field contains JSON data. I am getting syntax error when using below query -
SELECT * FROM products WHERE device_id = '1212'and product_id = '54'and option = '"{"229":"20"}"'
field option has value as {"229":"20"} I am getting 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 'option = '"{"229":"20"}"'
LIMIT 0, 25' at line 1
Please suggest any solution , thanks much!!!

option is a reserved keyword in MySQL. Use backticks to escape it or choose a different name
... `option` = ...

Related

MySQL SELECT record from database.table (database name contain '-')

I would like to get rows from another database so I created query:
SELECT * FROM database-test.users
MySQL result that error:
Database_Exception [ 42000 ]: SQLSTATE[42000]: Syntax error or access violation: 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 '-test.users' at line 1
How to solve this?
Thanks for reply
You need to do it like below (use back-ticks around table name):-
SELECT * FROM `database-test`.users
Or
SELECT * FROM `database-test`.`users`
I would recommend to 'back tick' all database and table names in your query. It will tell the database's SQL parser to ignore any special characters such as "-" and consider them as part of the name.
Example:
SELECT * FROM `database-test`.`users`
Try
SELECT * FROM `database-test`.users
As you can see, I have used the ` character to encapsulate database-test, which makes sure that non alpha-numeric characters, like - will be accepted in the name.

I want to select customer as well as date filter in sql

I have the query for select between dates by customer below but it shows error as
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 'brand1' in /path on line 92' at line 2
$query ="
SELECT *
FROM fuelinv
WHERE `billdate` BETWEEN '$from1' AND '$to1'
AND `cusname` = '$brand1'
";
Remove the '' around your variables. When using "" string in php you are free to use the variables without any special symbols.

Have trouble with mySQL query

I'm am working on a project and need to use the below query statement, unfortunately my table and column names have dashes. Does anyone know how to get this to work?
SELECT * FROM 'default-table' WHERE 'ds-avail'='Yes';
Here is the error I get.
Invalid query: 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 * FROM [default-table] WHERE [ds-avail]='Yes'' at line
Try this
SELECT * FROM `default-table` WHERE `ds-avail` = 'Yes';
You used 'table' while you should have it like this table
SELECT * FROM default-table WHERE ds-avail='Yes';
Be sure your table is named exactly "default-table" (without quotes)
Be sure the field that you're looking for is called "ds-avail" (without quotes) and it exists in that table.
You should put the table and the column without quotes:
SELECT * FROM default-table WHERE ds-avail='Yes';

What's wrong with this sql code?

If I remove the line condition=\''.$this->condition.'\', it works.
If I let it there, the following error message appears:
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 'condition='unknown', promotional='0', website='0', quantity='1',
' at line 7
mysql_query('UPDATE products SET
name = \''.$this->name.'\',
description = \''.$this->description.'\',
brand = \''.$this->brand.'\',
model = \''.$this->model.'\',
price=\''.$this->price.'\',
condition=\''.$this->condition.'\',
promotional=\''.$this->promotional.'\',
website=\''.$this->website.'\',
quantity=\''.$this->quantity.'\',
service=\''.$this->service.'\'
WHERE id = \''.$this->id.'\' '
CONDITION is a reserved mysql keyword. You must enclose it in backticks:
`condition`=\''.$this->condition.'\',
You have to rename condition column. See Reserved MySQL keywords table

SQL Error: SQL Syntax Error

Error Message:
SQL 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 'AND domain = 'ard.qc' AND snapshot_id = 2010 AND locale = 'en_US'' at line 1
SQL Query:
SELECT
entity_id,
content_id
FROM collateral_cms_mapping
WHERE entity_id IN ({$entity_ids})
AND domain = '{$this->getSite()->getInternalId()}'
AND snapshot_id = {$this->getSnapshotDao()->getCurrentSnapshot()}
AND locale = '{$locale}'
Actual SQL after the value is replaced and string concatenation:
SELECT
entity_id,
content_id
FROM
collateral_cms_mapping
WHERE
entity_id
IN
()
AND
domain = 'ard.qc'
AND
snapshot_id = 2009
AND
locale = 'en_US'
Any suggestions?
What's the value of $entity_ids? Probably it's empty, so your query contains IN () which is invalid.
You do not have to concatenate your strings.
It looks like the prepared query is faulty somewhere in the IN ({$entity_ids}).
Echo the query string to check your IN statement.
Definitely look at the IN statement. While I'm not familiar with MySQL, I don't believe an empty IN () clause is permitted in DB2 or Oracle (although I won't swear to it).

Categories