I stored a field of records as JSON "[1,2,3,14,4]"
In MySQL 5.7, I used JSON_LENGTH to count it.
But How can I count in MySQL 5.5.
This is my example query in MySQL 5.7.
SELECT JSON_LENGTH(view_cnt) AS view_cnt FROM `tbl` WHERE `published`=1
Since mysql v5.5 does not have any json support, you need to get creative using string functions.
If your json is simple and does not have values that themselves contain comma, then just count then number of commas within your string and add 1 to it:
select char_length(json_field)-char_length(replace(json_field,',',''))+1 from table
If your json values contain commas, then you would have to write a json parser in mysql to get the length.
Related
Is there anyway to pass JSON list to mysql stored procedure and insert in that sp?
It has to get the data from the list and insert as separate rows with individual columns.
MySQL has a JSON field type, but it does not understand JSON. MySQL only understands SQL. When you input data into MySQL, it has to be in SQL.
You could use another language to interpret the JSON and convert it to SQL.
I tried to add a json column to my database by using phpMyAdmin
but Unfortunately, phpMyAdmin converts the json column to Longtext type
So, I'm asking about the ability to use the JSON Where Clauses with this type
https://laravel.com/docs/5.7/queries#json-where-clauses
You cannot use those queries on non-JSON data types in MariaDB. And as of 10.2, it doesn't officially support it.
You can use the JSON helper functions to query against data (ie: where JSON_CONTAINS(...) and others.
You can also create columns that are extracted values from the JSON data using Virtual Columns
Here's a good post with much more detail.
I am migrating a database with geometric data from MySQL to PostgreSQL. I've run into a snag because the POINT datatypes are incompatible between the two databases.
In MySQL, the POINT values are single hex strings like 0xE21D4B40. However, I cannot insert these into PostgreSQL because PostgreSQL's POINT datatype expects coordinates in the form of (x,y). What is the best way to handle this conversion?
#tadman's comment ended up being my solution: unpacking the hex string to a pair of values on the MySQL side. In my case the hex string was a column called location in the city table, so I used the query SELECT ST_AsText(location) FROM city;.
This returned rows like POINT(-133.03531 54.23346) which PostgreSQL was happy to accept.
I'm querying a MySQL DB with some strings in one field which contain apostrophes which I cannot remove or escape when adding to the DB. How do I format a query not to fail on a string containing an apostrophe? For example, doing a query against a FULLTEXT indexed field:
"SELECT * FROM NationalTideStationsA WHERE MATCH(location) AGAINST('$myState')";
This fails whenever the returned string has an apostrophe, for example, when the location field contains:
"Cedar Tree Neck, Martha's Vineyard, Massachusetts"
all queries for locations in Massachusetts fail.
I cannot work out if SQL offers a way to format the query to cope with that.
The SELECT query works just as desired otherwise.
Agreed on the suggestion to read up on sql injection. For the immediate, replace all single quotes with two single quotes.
I have this MySQL query, and it works on a MySQL database, but not on a PostgreSQL one:
select setype from _entity where id='72#78|'
Now, what exactly is '72#78|' trying to do? id is an integer field, so when the query is run on a Postgressql DB, it gives an invalid input syntax for integer: "72#78|" error.
I know that | is a bitwise OR operator, but what exactly is being ORED here? And, just as importantly, what is the # for? I tried to look for it in the MySQL manual, but due to sub-par searching skills, I couldn't find it.
When I run the above query on a MySQL DB, it finds data with an id value of 72,somehow, the expression evaluates to the first number.
So, what is the above query trying to do, and how do I convert it into a PostgreSQL equivalent?
Thanks for all your help, have a good day.
MySQL is converting the text string to a numeric, and ignoring anything after the first non-numeric character, therefore, it is just comparing id = 72, which is what you are getting from the output.
My guess is that PostgreSQL is trying to convert the whole string to an integer and failing because it isn't a valid integer value.
To do the equivalent in PostgreSQL, you would need to convert the 72#78| to a simple integer before running your query.