MySQL Query Using JSON_CONTAINS - mysql

I have a MySQL 5.7 table which includes a column of type VARCHAR named 'area' which contains JSON data. For example
["BS20","BS21"]
I need to search on that table to find the first row containing a particular string. The string is contained in a variable like
$area = BS20;
and I have tried this for my query
SELECT * FROM aedv2_admin WHERE JSON_CONTAINS(area,'$area')=1
and I get an error
Invalid JSON text in argument 2 to function json_contains: "Invalid value." at position 0.12
For testing I have tried hard coding argument 2 as BS20 and 'BS20' but neither helps. What am I doing wrong?

Phew! That was a bit painful, but with thanks to Akina my final code ended up like this
$area = '\"'.$_POST['area'].'\"';
$query_sponsor = "SELECT * FROM aedv2_admin WHERE JSON_CONTAINS(area,'$area')";

Related

In mysql where clause bigint showing unexpected result

Hope you all are doing well.
Today morning I was writing some sql query and I found a situation where I need your suggestion on that so here is the situation:
I've a table in mysql called users it has id column which is bigint
now when I'm trying to extract data with a query like :
select * from users where id = 123
in this case it'll show the result for user 123
now the situation here is if I run the same query like:
select * from users where id = 123b
now the issue here is it is still giving me the data for user 123
need your suggestion guys on the same, I did some R&D on the same but didn't found much usefull.
Thank you
Your question is tagged with mysqli which means you are using PHP to build your query. If you pass the string 123b as an integer, PHP will keep the leading digits:
var_dump((int)'123b');
# output: int(123)
MySQL then execute your query with this well formed integer 123.
If you execute a raw SQL query using any tool of your choice you should get an error like the following:
Unknown column '123b' in 'where clause'
This is because 123b cannot be an integer as it contains a letter, and isn't a string as it has no string delimiters.
Note that MySQL can still interpret strings without delimiters like hexadecimal values. For example, 0x31323362 is a valid string value for 123b and does not need quotes around it.

type error when trying to update a table field name query

I have a query where i am trying to update field by taking the left number of characters before a space.
I'm not very good with Access VBA, so I'm trying to do this via a query.
my data is a list of SKUs, where I want to update the same field (sku) with a shorter SKU number, by using the Left$ function along with the InStr function to take all characters to the left of a space in the number.
test sku
E349CAJ6 OBROBRO
E357CAJ6 OBROSID
E329CAJ6 OWHIBRO
E358CAJ6 ONO SID
Note that the space isn't always in position 9, sometimes it varies. I was trying to use the following Query update value: Left$([IMPORT - EFF ORDERS]![SKU], InStr([IMPORT - EFF ORDERS]![SKU]," ",1))
The InStr, identifies the starting position based on the space, to use for the Left function.
The SKU field is a Short Text type field.
However, when I run the query, I get a "Type Conversion" error and none of the records will update.
I have wracked my brain to try to figure this one out and would appreciate an expert's fresh eyes on it.
Thank you so much in advance !
The ,1 is in wrong argument, really don't need it. If you want to use Compare argument then also use Start argument. Without explicit Start and Compare parameters, function will use defaults.
Left([IMPORT - EFF ORDERS]![SKU], InStr(1, [IMPORT - EFF ORDERS]![SKU], " ", 1))
InStr() in this case is returning a value that will be used as a length parameter, not a starting position. Start position for Left is first character.

MySQL full text search on JSON data

I'm trying to replicate the following LIKE query using a full text search on JSON data;
SELECT * FROM table
WHERE response LIKE '%"prod_id": "foo"%'
AND response LIKE '%"start_date": "2016-07-13"%'
In my database the above query returns 28 rows
This is my attempt:
SELECT * FROM table
WHERE MATCH(response)
AGAINST('+"\"prod_id\": \"foo\"",+"\"start_date\": \"2016-07-13\""')
However this returns over 4,500 rows (the same as running the first query for only the prod_id ~1,900 rows when running the first query on just the date)
It was my understanding that +"text here" would indicate a required word, and that literal double quotes (present in the JSON data) should be escaped, and that , would indicate a split between the two strings I'm looking for. What am I not understanding correctly? Is there any point in running this as a full text query anyway?
Thanks to #Sevle I've tweaked my query like so, and it's returning the correct results;
SELECT * FROM table
WHERE MATCH(response)
AGAINST('+\"prod_id: foo\" +\"start_date: 2016-07-13\"' IN BOOLEAN MODE)
The comma was not helping and I was escaping the wrong characters, and of course I did need IN BOOLEAN MODE to be added. Finally, I removed the double quotes I was searching for in the JSON string.
It may also be worth noting that as I'm using PHP PDO to run this query I also had to make the following tweaks.
Instead of constructing the query like so trying to bind the variables like I normally would;
$query = $db->prepare('...AGAINST('+\"prod_id: :prod_id\" +\"start_date: :start_date\"');
$query->execute(array('prod_id' => 'foo', 'start_date' => '2016-07-13'));
I had to do this, as I found I could not bind variables in full text searches
$sql_against = $db->quote('...AGAINST('+\"prod_id: foo\" +\"start_date: 2016-07-13\"');
$query = $db->prepare("...AGAINST($sql_against IN BOOLEAN MODE)")

How do I convert binary data from MySQL to a string in ColdFusion 11?

I have a field in my MySQL database that is set to data type "BIT". The value in the field is 101101. I am trying to read this value using Coldfusion (version 11)
I simply use the following code:
<cfquery name=q1 datasource=#data_source#>
select * from mytable
</cfquery>
<cfoutput>
#q1.mybitfield#
</cfoutput>
I have tried using CAST and CONVERT on the MySQL side and I have tried CharSetEncodeing on the CF side along with every option of ToString, ToBase64, and ToBinary that I can think of.
I still can not get my output to look like it does in the database.
Actually, I was probably thinking of SQL Server's bit type. For MySQL, a simpler option might be to use the bin() function which:
Returns a string representation of the binary value of N, ...This is equivalent to CONV(N,10,2). Returns
NULL if N is NULL.
For example:
SELECT bin(YourBitColumn) AS YourBitColumn FROM YourTable
... or
SELECT bin(YourBitColumn+0) AS YourBitColumn FROM YourTable
NB: High-order 0 bits are not displayed in the converted value. That applies the CF function as well.
Thanks Leigh for your help. I was never able to get it to work with just one step so I used a two step solution (if you want to call it that). What I ended up doing was setting up my MySQL statement to convert the field to an Unsigned interger (although Decimal or a signed integer would have also worked), then once I read in the decimal value I was able to convert it to binary using your suggestion of FormatBaseN(q1.myfield,2). So thanks for the reminder of FormatBaseN. I had forgotten about that one.
My final code ended up looking like this:
The MySQL statement:
SELECT *, CONVERT(item , UNSIGNED) as di from mytable
And the Coldfusion looked like this:
<cfset d = FormatBaseN(q1.di, 2)>
Edit
After writing this, I decided to go with Leigh's answer above since it was a better solution.

How can I order a mySQL column by a number imbedded in a string?

I am parsing genomic positions from a MySQL field. The field is called "change" and the entries are of the form:
g.100214985T>C
g.100249769C>A
g.10185G>T
I am trying to order the field by the numerical portion of the string. I am trying to figure out what mySQL query I can use to accomplish this. I have tried using REGEXPs and SUBSTRING_INDEX but am still running into issues. Any help would be much appreciated!
Assuming you have always 2 characters in front of and 3 at the end you need to have removed:
SELECT CAST(SUBSTR(col from 3) AS UNSIGNED) AS value
FROM `my_table`
ORDER BY value
Watch this sql fiddle also: http://sqlfiddle.com/#!2/7bc0e/67
Thank you #MarcusAdams and #amoudhgz! The following code works:
CAST(SUBSTR(field, 3) AS UNSIGNED).
MySQL already stops the conversion at the first non-numerical character.