Find JSON nth value using mysql query - php

I have this JSON values in MySQL database
Conditions
1- >
[{"offer_id":"158","offer_start":"2017-12-17 09:21:27","offer_ends":"2017-12-17 10:21:27"},{"offer_id":"167","offer_start":"2017-12-17 12:28:57","offer_ends":"2017-12-17 12:58:57"}]
2 ->
[{"offer_id":"170","offer_start":"2018-01-17 04:26:26","offer_ends":"2018-01-17 05:11:26"},{"offer_id":"167"}]
3 ->
[{"offer_id":"170","offer_start":"2017-12-11 20:49:12","offer_ends":"2017-12-11 20:49:12"}]
3 ->
[{"offer_id":"170"}]
So I need to get the nth offer_ends value like "2017-12-11 20:49:12" using mysql query

Considering that you need to find the data from table1 where the column name offers. You need to fetch data where the "offer_ends" has value 2017-12-11 20:49:12
SELECT JSON_CONTAINS(offers, '2017-12-11 20:49:12', '$.offer_ends')
FROM table1;
Get all the records:
SELECT offers, JSON_EXTRACT(offers, "$.offer_ends")
FROM table1
If you want then you can apply the order by on any column and then get the Top 1 record to get the last record.

Here i have one code which get all the offer_ends value by using MySQL -> SELECT REPLACE(REPLACE(JSON_EXTRACT('[{"offer_id":"23","offer_start":"2017-12-11 20:49:12","offer_ends":"2017-12-11 20:49:12"},{"offer_id":"23","offer_start":"2017-12-11 20:49:12","offer_ends":"2017-12-11 20:49:12"}]', '$[*].offer_ends'),'[',''),']','') as offer_dates
and the result is -> "2017-12-11 20:49:12", "2017-12-11 20:49:12" which shows all offer end dates
So i need to find the nth offer_ends value like this -> "2017-12-11 20:49:12"

Guys i found the solution
SELECT JSON_EXTRACT('[{"offer_id":"158","offer_start":"2018-02-21 13:03:15","offer_ends":"2018-02-21 14:03:15"},{"offer_id":"170","offer_start":"2018-02-21 15:03:15","offer_ends":"2018-02-21 16:03:15"}]',CONCAT("$[",JSON_LENGTH('[{"offer_id":"158","offer_start":"2018-02-21 13:03:15","offer_ends":"2018-02-21 14:03:15"},{"offer_id":"170","offer_start":"2018-02-21 15:03:15","offer_ends":"2018-02-21 16:03:15"}]')-1,"].offer_ends")) as offer
Thank you guys

Related

How to check column in array in Mysql

Sorry if the topic name is not reasonable.
I have a column in mySql with value (10,11,12), it's a string, like this
And I have an array to check, how can select this row if array contains any one value 10, 11 or 12. For example:
array(3,4,11) - choose
array(5,6,7) - not choose
array(11,8,10) - choose
Thank you!
Here is the query :
SELECT *
FROM your_table
WHERE FIND_IN_SET(10, custom_categories)
OR FIND_IN_SET(11, custom_categories)
OR FIND_IN_SET(12, custom_categories);
For More Detail : Read

MySQL query to return json name value

I'm working with a table in which information is stored in a table in JSON format. The JSON value field looks like:
select * from k2_extra_fields where id = 2 and published = 1;
id | value
2,[{"name":"Apples","value":1,"target":null,"alias":"","required":0,"showNull":1},{"name":"Pears","value":2,"target":null,"alias":"","required":0,"showNull":1},{"name":"Mangos","value":3,"target":null,"alias":"","required":0,"showNull":1},{"name":"Guava","value":4,"target":null,"alias":"Fruit","required":0,"showNull":1},{"name":"Pineapple","value":5,"target":null,"alias":"Fruit","required":0,"showNull":1}]
Or values in a simple line by line view (minus the ID):
[
{"name":"Apples","value":1,"target":null,"alias":"","required":0,"showNull":1},
{"name":"Pears","value":2,"target":null,"alias":"","required":0,"showNull":1},
{"name":"Mangos","value":3,"target":null,"alias":"","required":0,"showNull":1},
{"name":"Guava","value":4,"target":null,"alias":"Fruit","required":0,"showNull":1},
{"name":"Pineapple","value":5,"target":null,"alias":"Fruit","required":0,"showNull":1}
]
The query that leads me here returns the value of 3. 3 = Mangos. How do I take the '3' value and match it up with the stored names/values so that I end up with the output, Mangos?
It should be possible with build in mysql functionality, but very hard and 'not clever' idea to do. If you really need to compute this problem within mysql, you would need to actually add new funtionality to your mysql. Look up on UDF plugins: http://dev.mysql.com/doc/refman/5.1/en/udf-compiling.html

PostgreSQL multiple criteria statement

I have the following postgreSQL statement that returns dates that school is open and attendance needs to be taken
SELECT school_date
FROM attendance_calendar
WHERE syear='$syear'
AND school_date<CURRENT_DATE
AND calendar_id='$cal_ID'
I then have another table called attendance_completed which has a column called school_date that i want to query for each returned date from the above statement. But instead of doing two different statements, just wondering if someone can help combine the statement?
Basically, i would like the above statement to check if the returned school_date exists in the table called attendance_completed, and only return the dates that are missing.
Thanks
This should work for you:
SELECT cal.school_date
FROM attendance_calendar cal
WHERE cal.school_date NOT IN (
SELECT com.school_date
FROM attendance_completed com
WHERE com.school_date IS NOT NULL
) AND
cal.syear = '$syear' AND
cal.school_date < CURRENT_DATE AND
cal.calendar_id = '$cal_ID;

MySQL COUNT showing it's working out

I would expect the following to output the number "5", since there are 5 rows in the database with with item 68 and user 1. But instead I'm getting this output "12345".
$resultb4 = mysql_query("SELECT COUNT(comparedRating) FROM recComparedRating WHERE user1='1' AND itemID='68' GROUP BY itemID AND user1");
while($rowb4 = mysql_fetch_array($resultb4)){
$countcomparedratings=$rowb4['COUNT(comparedRating)'];
}
echo $countcomparedratings;
What am I doing wrong?
The reason you are getting 12345 is because your query is returning 5 results and your code to output the count is simply outputting the concatenation of the returned array from the query.
Without understanding your database structure, I'm guessing that the reason you're getting the '12345' has something to do with your GROUP BY clause. Use a program like MySQLWOrkbench to connect to your database and test out your query before you include it into your code. It is a time saving technique to debug your queries.
Also, I would alias the COUNT value so that you simply refer to the alias when you refer to your column names.
SELECT COUNT(comparedRating) as ratingCount FROM recComparedRating WHERE user1='1' AND itemID='68' GROUP BY itemID AND user1");

Mysql query help - count rows

I have two different tables trackingevent and link
Trackingevent looks like
eventName|eventStamp
SHARE_FACEBOOK|2011-01-20 14:05:40
SHARE_TWEET|2011-01-20 14:47:57
SHARE_FLICKR|2011-01-20 15:08:58
SHARE_STATION_LOGIN|2011-01-20 15:09:09
EMAIL_SHARE|2011-01-20 15:10:13
CONTEST_ENTRY:BLAH DATA|2011-01-20 15:10:13
CONTEST_ENTRY:BLAH DATA|2011-01-20 15:10:13
and link looks like
id|emailSub
6|1
7|0
8|1
9|0
And what I need to do is I need to count all the SHARE_FACEBOOK
and all the SHARE_TWEET
and all the SHARE_FLICKR
and all the SHARE_STATION_LOGIN
and all the COPNTEST_ENTRIES (Without the rest of the data after the :)
AND EMAIL_SHARE
and somehow combine those with the amount of emailSub (equals to 1)
and amount of id.
So I get a returned array sort of like
EMAIL_SHARE 77
SHARE_FLICKR 9
SHARE_FACEBOOK 105
SHARE_STATION_LOGIN 223
SHARE_TWEET 18
# of ID's
# of emailsub=1
CONTEST_ENTERIES 550
I can get the first part of it using
SELECT eventName, COUNT(*) FROM trackingevent GROUP BY eventName
But I am confused with how to get the # of ID's in the link the # of emailsubs=1 and the number of CONTEST_ENTRY:BLAH DATA (the blah data changes).
Would I have to do three different sql queries and combine the data? Or could I somehow combine them into a single one or?
This should give you the count of each type using the prefix
SELECT LEFT(eventName,INSTR(CONCAT(eventName,':'),':')) as prefix, count(*)
FROM trackingevent
GROUP BY LEFT(eventName,INSTR(CONCAT(eventName,':'),':'))
You'll have to answer the questions in the comments if this isn't what you were asking for

Categories