i have one mysql table name called mem_exam_dates.
+---------+-------------+----------------------------------+
| rec_id | mem_name | exam_dates |
+---------+-------------+----------------------------------+
| 1 | Raju | 2015-01-05,2015-05-09,2018-05-09 |
| 2 | Rajes | 2015-10-05,2015-12-09,2018-09-09 |
+---------+-------------+----------------------------------+
now i want to display the result as below.
+-------+---------------+
| Raju | Exam Dates |
+-------+---------------+
| 2015-01-05 |
| 2015-05-09 |
| 2018-05-09 |
+-----------------------+
i am writing the query like
select * from mem_exam_dates where rec_id=1
from the above query i am getting total exam dates as single string.
but i want the exam dates as below.
+----------------+
| 2015-01-05 |
| 2015-05-09 |
| 2018-05-09 |
+----------------+
what is the query for that one?
if anybody knows let me know...
Thanks in advance
kalyan
In MySQL you can extract a part from string with SUBSTRING_INDEX.
So you can try the follow SQL
SELECT SUBSTRING_INDEX(exam_dates, ',', 1) as first
, SUBSTRING_INDEX(SUBSTRING_INDEX(exam_dates, ',', 1), ',', -1) as second
, SUBSTRING_INDEX(exam_dates, ',', -1) as third
In .NET you can use split()
https://msdn.microsoft.com/de-de/library/b873y76a%28v=VS.110%29.aspx
If you have the column in a variable named exam_dates you can use:
string [] dates = exam_dates.Split(new Char [] {','});
So you have an array of all dates.
You need to process the data in the server side language. If you are using PHP, you would use explode.
For example:
$dates= explode(",", $longdatestring);
echo $dates[0]; // date1
echo $dates[1]; // date2
To find the number of dates returned, simply count the elements in your array.
$numberofdates = count($dates);
Or look through the array untill you run out of elements.
foreach ($dates as $date) {
echo $date;
}
For further details in PHP:
https://secure.php.net/manual/en/function.explode.php
Related
I have a SQL table that looks something like this:
| ID | Value |
| --- | ----------------------------------------------------- |
| 1 | {"name":"joe", "lastname":"doe", "age":"34"} |
| 2 | {"name":"jane", "lastname":"doe", "age":"29"} |
| 3 | {"name":"michael", "lastname":"dumplings", "age":"40"}|
How can I using SQL select function, select only the rows where "age" (in value column) is above 30?
Thank you.
The column Value as it is it contains valid JSON data.
You can use the function JSON_EXTRACT() to get the the age and convert it to a numeric value by adding 0:
SELECT *
FROM tablename
WHERE JSON_EXTRACT(Value, "$.age") + 0 > 30;
See the demo.
Imagine a table which tracks baseball pitchers like so...
+------------+--------------------+-------+
| id | name | secondary_pitch |
+------------+--------------------+-------+
| 13 | Chris Sale | ['Curveball','Slider'] |
| 14 | Justin Verlander | ['Fastball','Changeup'] |
| 15 | CC Sabathia | ['Fastball','Curveball'] |
| 16 | Sonny Grey | ['Slider'] |
| 17 | Aldoris Chapman | [] |
+------------+--------------------+-------+
Notice the secondary_pitch column has a JSON value. So if a pitcher, like Chapman, has no secondary pitch, it will not return null, instead it returns an empty JSON string ('[]').
How then can I get a count of the number of pitchers who have no secondary pitch?
I can't do...
select count(*) from pitchers where secondary_pitch is null
I think you can just use json_length():
where json_length(secondary_pitch) = 0
You could use JSON_EXTRACT to get first value from your column and check for not null
where JSON_EXTRACT(`secondary_pitch`, '$[0]') is not null
Demo
I see this is not answering original question of matching against empty array ([]) but this has worked for me, matching against empty dictionary ({}), at mysql 5.7.20-0ubuntu0.16.04.1 - (Ubuntu).
I used JSON_OBJECT function but it is very likely the JSON_ARRAY will also work in similar way, creating the 'empty' object when called without arguments.
If I wanted to match against the json column vmkeys value of {} (empty dictionary), I used the following query:
SELECT vmkeys FROM `labinstances` WHERE vmkeys=JSON_OBJECT()
To match against the vmkeys value of NULL, I used this:
SELECT vmkeys FROM `labinstances` WHERE vmkeys is NULL
Hope this helps...
This will check where secondary_pitch is (null) or '' (empty string)
SELECT count(*) from pitchers WHERE secondary_pitch IS NULL OR secondary_pitch = '';
also you can use like this.
SELECT count(*) from pitchers WHERE secondary_pitch LIKE '%[]%'
You can use the below function
SELECT * FROM TABLE_NAME WHERE
JSON_SEARCH(secondary_pitchers, 'all', '')
IS NOT NULL;
I have a table with 2 Columns, filled with strings
CREATE TABLE [tbl_text]
(
[directoryName] nvarchar(200),
[text1] nvarchar(200),
[text2] nvarchar(200)
)
The Strings are build like the following
| Text1 | Text2 |
|------------|----------|
|tz1 tz3 tz2 | al1 al2 |
| tz1 tz3 | al1 al3 |
| tz2 | al3 |
| tz3 tz2 | al1 al2 |
Now i want to Count how many times the TestN or TextN are resulting in the
| Text1 | al1 | al2 | al3 |
|-------|------|------|------|
| tz1 | 2 | 1 | 1 |
| tz2 | 2 | 2 | 1 |
| tz3 | 3 | 2 | 1 |
i tried solving it with an sql-query like this:
TRANSFORM Count(tt.directoryName) AS Value
SELECT tt.Text1
FROM tbl_text as tt
GROUP BY tt.Text1
PIVOT tt.Text2;
This works fine if i got fields only with one value like the third column (the complete datasource has to be like a one-value-style)
But in my case i'm using the strings for a multiselect...
If i try to conform this query onto a datasource filled with the " " between the values the result is complete messed up
Any suggestions how the query should look like to get this result ?
You'll have to split the strings inside Text1/Text2 before you can do anything with them. In VBA, you'd loop a recordset, use the Split() function and insert the results into a temp table.
In Sql Server there are more powerful options available.
Coming from here: Split function equivalent in T-SQL? ,
you should read this page:
http://www.sommarskog.se/arrays-in-sql-2005.html#tablelists
I have an issue with a mysql subselect.
**token table:**
id | token | articles
1 | 12345 | 7,6
2 | 45saf | 6,7,8
**items table:**
id | name | filename
6 | Some brilliant name | /test/something_useful.mp3
7 | homer simpson | /test/good-voice.mp3
**query:**
SELECT items.`filename`,items.`name` FROM rm_shop items WHERE items.`id` IN ( SELECT token.`articles` FROM rm_token token WHERE token.`token` = 'token')
I only get one of the two files (with the id 7 that is). What am I missing here?
For a column with concatenated data (like your "articles" column), you can not use MySQL IN() Function. Instead use the string function FIND_IN_SET() to query such values. In your case:
SELECT items.`filename`,items.`name` FROM rm_shop items
WHERE FIND_IN_SET(items.`id`,
(SELECT token.`articles` FROM rm_token token WHERE token.`token` = 'token')) > 0
A working sqlfiddle: http://sqlfiddle.com/#!2/796998/3/0
I am having a table with data stored in row basis as shown below.
UID | DetailsID | Data|
----------------------|
1 | 1 | A |
1 | 2 | 200|
1 | 3 | 2010-10-11 08:32 |
2 | 1 | B |
2 | 2 | 600|
2 | 3 | 2011-05-20 14:56 |
From this I need the output as follows
UID|1|2|3
------------
1|A|200|2010-10-11 08:32
2|B|600|2011-05-20 14:56
Here main thing is, the number of entries of DetailsID values is not known.
I wanted this one in MySQL.
Please help me out of this.
Not quite what you want, but other than loads of left joins i can only suggest:
SELECT UID,GROUP_CONCAT(DetailsID SEPARATOR ",") "DetailsIDs",GROUP_CONCAT(Data SEPARATOR ",") "Data" FROM data_table GROUP BY UID;
Do that transformation in your coding language, not in SQL.
you didnt say where you need the output. If you need the output in PHP pages it is simple only by creating the loop for the entries in columns wise.