I am wanting to search for text within brackets and speech marks. The values in the column look like this: ["flour","butter","eggs"]
I have tried:
SELECT * FROM 'posts' WHERE ingredients = '''butter'''
Th, however,er does not work.
How can I write a query to return the rows only containing the desired words? All help is much appreciated.
Ps. I know the data should not have been stored like this and it is not in the correct normal form. I just need a way to search for this for now.
Well, if this is JSON, you should store it as JSON, not a string.
That said, you can use LIKE:
SELECT p.*
FROM posts p
WHERE ingredients LIKE '%"butter"%';
Some comments about quotes. You seem very confused by quotes in your query.
You have 'posts' as a table name in single quotes. This will actually return an error. The only quoting allowed here are escape characters (backticks). However, they are not needed.
Your data has double quotes around the values. However, you have doubled up single quotes in your comparison string. Single quotes and double quotes are different things.
Related
I'm currently using PDO connection for perform some mysql queries and since I use the command $conn->prepare("HERE THE QUERY") I want to know how to format characters like quotes and double quotes.
When I have cases like this one:
$conn->prepare("SELECT * FROM ('SELECT DISTINCT (user_id) FROM table1')");
This is fine because in the nested SELECT there isn't a particular character that can cause problems. But how can we handle special cases like that?
Here a strange example (forget the mysql.. this is quite irrelevant, focus on the quotes situation) with quotes and double quote inside the nested SELECT:
$conn->prepare("SELECT * FROM ('SELECT user_id, CONCAT('[\"",GROUP_CONCAT(DISTINCT(cat) ORDER BY user_id DESC SEPARATOR "\",\""),"\"]') cat_grouped FROM table_1') select1");
What should be the right quotation mark syntax according to this example query? If i use ' instead of " when I prepare the query the problem is quite fixed, but I want to understand if there is a smart way to maintain the double quotes.
firstly I recommend using single quotes - they're faster :D
The main issue is using the same quotes with each other. Doing this causes pre-mature closing, and I'm sure you'd like to save that pre-mature embarrassment.
See in simple terms:
"string has star"ted"
As you can see, the first double quote the file gets to is the one after star. This closes the string after star, rendering the ted" in a fatal error.
What you want to do is escape the quotes that conflicts with the opening quote. Single quotes inside double quotes are fine, and vice versa.
Escape single quotes inside single quotes and double quotes inside double quotes - the rest should be ok to leave. Also I recommend using backticks for your mysql tables and fields to avoid some errors down the road if they deiced to add some new keyword that just so happens to match your table/field name
e.g.
if using single quotes:
$conn->prepare('SELECT * FROM table WHERE string_field = \'value\'');
if using double quotes:
$conn->prepare("SELECT * FROM table WHERE string_field = \"value\"");
if mixing:
$conn->prepare('SELECT * FROM table WHERE string_field = "value"');
\ is the escape character used for situations like this :)
The alternative is concatting strings:
$conn->prepare('SELECT * FROM table WHERE field = '. $foo);
so breaking it up, you declare string same was as usual with preferred quotes, then to add stuff to it, you use . to concat
I'm attempting to query our MSSQL database but I'm getting no data when there clearly is data there.
First I query
SELECT id, instruction_link FROM work_instructions WHERE instruction_link LIKE "%\\\\cots-sbs%";
Which returns 100+ lines.
http://tinypic.com/r/ief8td/8
(sorry couldn't post as actual picture, don't have enough rep :(
However if I query
SELECT id, instruction_link FROM work_instructions WHERE instruction_link LIKE "%\\\\cots-sbs\\%";
http://tinypic.com/r/33ksw3q/8
I get no results with the 2nd query. I have no idea what I'm doing wrong here. Seems pretty simple but I can't make any sense of it..
Thanks in advance.
As documented under LIKE:
Note
Because MySQL uses C escape syntax in strings (for example, “\n” to represent a newline character), you must double any “\” that you use in LIKE strings. For example, to search for “\n”, specify it as “\\n”. To search for “\”, specify it as “\\\\”; this is because the backslashes are stripped once by the parser and again when the pattern match is made, leaving a single backslash to be matched against.
\\% is parsed as a string containing a literal backslash followed by a percentage character, which is then interpreted as a pattern containing only a literal percentage sign.
I converted a large amount of data from mssql to mysql and a few of them came in with the 'other' double quotes ... the sharper ones.
I would like to do a query in phpmyadmin for all the entries that have that symbol because its breaking my query (coming back as null) but cannot figure out how to write it ...
SELECT * FROM table where id LIKE '% %' <-- i dont actually know what the ascii symbol is for that one and this html ascii convention doesnt work anyway.
If you find one of them, can't you just copy it and paste it into your LIKE clause? That will prevent you from having to figure out the ASCII code for it.
I'm trying to pivot a table so I can output the data as a CSV. I need to do something like this:
SELECT .... t1.`column_one`, t1.`column_two`, ...
Problem is that some of the columns are expected to contain commas, single quotes, and double quotes.
Is there a way to make something like this work:
SELECT .... t1.`foo's, "bar"`, ...
The above doesn't work. Suggestions?
I've tested and can confirm that the following definitely works:
SELECT `t1`.`foo's, "bar"` FROM `t1`;
The only thing I could suggest is to place the table name between ` (backtick) characters.
According to the MySQL documentation you should be fine as long as the column name is ASCII, doesn't contain an ASCII NUL (0x00) and is under 64 characters long in total. What do you mean by "doesn't work"? Does it give an error message?
I'm using VBA that encloses every field with single quotes in order to import into mySQL and noticed that every variation I tried on NULL for numeric data types imported as 0. If I drop the backticks the nulls import fine, so I think my question boils down to this: if there any way to indicate NULL data inside of single quotes in a mySQL import?
this website shows a bunch of examples, but nothing inside of single quotes.
thanks!
If the string inside the single quotes is escaped, too, then there is no way of making it NULL. That would negate the purpose of quotes+escaping.
If it is not escaped, you could "break out" of the single quotes with a single quote. For example if you store the string '/0+' it will become ''/0+'' (say: empty string divided by zero plus empty string) which is NULL because anything divided by zero is NULL.