I have a database with table1
table1
Output should be in this format
table2subject per students can change
Something like this will work, but with different no of comma separated values I am not sure how to do
SELECT Column1,
substr(Column2, 1, length(Column2) - length(substring_index(Column2, ',', -2))) Column2,
substring_index(substring_index(Column2, ',', -2), ',', 1) Column3,
substr(trim(substring_index(Column2, ',', -1)),1,2) Column4,
substring_index(Column2, ' ', -1) Column5
FROM table
Related
I want export data from my SQL database.
Simply use :
SELECT `id`,`tags` FROM `posts`
This query give me those results :
(1, 'handshake,ssl,windows'),
(2, 'office,word,windows'),
(3, 'site')
I want results in this form:
(1, 'handshake'),
(1, 'ssl'),
(1, 'windows'),
(2, 'office'),
(2, 'word'),
(2, 'windows'),
(3, 'site')
How can write a query that give me this results?
Thank you and sorry for my poor English.
If you are using SQL Server
You can apply the fuction
STRING_SPLIT
SELECT id, value
FROM posts
CROSS APPLY STRING_SPLIT(tags, ',')
Check this out:
SQL Fiddle example
After many search and try finally i find the solution:
SELECT
DISTINCT postid , SUBSTRING_INDEX(SUBSTRING_INDEX(tags, ',', n.digit+1), ',', -1) val
FROM
posts
INNER JOIN
(SELECT 0 digit UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6) n
ON LENGTH(REPLACE(tags, ',' , '')) <= LENGTH(tags)-n.digit;
For a max of three words, the code below can be used. If you want more words then you just add more lines. The method may not be fully automated, but it works.
SELECT id, SUBSTRING_INDEX(SUBSTRING_INDEX(tags, ',', 1), ',', -1) FROM tabela
UNION
SELECT id, SUBSTRING_INDEX(SUBSTRING_INDEX(tags, ',', 2), ',', -1) FROM tabela
UNION
SELECT id, SUBSTRING_INDEX(SUBSTRING_INDEX(tags, ',', 3), ',', -1) FROM tabela
ORDER BY id;
Below is the MySQL data table in which we have 5 lookup columns and their respective names in lookup_list column separated by commas. I want to select each lookup column name separately from lookup_list column like:
Invoice_Lookup as Lookup_1 Name
Leadlist_Lookup as Lookup_2 Name
and so on.
You should normalize your data.
But by using SUBSTRING_INDEX you can split your Lookup_list into columns like so
SELECT
SUBSTRING_INDEX(SUBSTRING_INDEX(Lookup_list, ',', 1), ',', -1) AS Lookup_1
, SUBSTRING_INDEX(SUBSTRING_INDEX(Lookup_list, ',', 2), ',', -1) AS Lookup_2
, SUBSTRING_INDEX(SUBSTRING_INDEX(Lookup_list, ',', 3), ',', -1) AS Lookup_3
, SUBSTRING_INDEX(SUBSTRING_INDEX(Lookup_list, ',', 4), ',', -1) AS Lookup_4
, SUBSTRING_INDEX(SUBSTRING_INDEX(Lookup_list, ',', 5), ',', -1) AS Lookup_5
FROM
[table]
How do I split Group_Concat() result into different columns? I don't want to show it in the same cell separated by comma.
You can use SUBSTRING_INDEX twice after wrapping it in a subquery, in case you know the delimiter.
SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(SomeColumn, '.', 3), '.', -1)
FROM (
SELECT GROUP_CONCAT(...) AS SomeColumn
FROM ...
) AS SomeTable
The first SUBSTRING_INDEX gets the string from first -> n
The second SUBSTRING_INDEX removes the string from first -> n - 1
Check this SQLFiddle example
In your case, if you know the number of columns, your query would look something like (assuming you have 5 strings separated by comma):
SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(SomeColumn, '.', 1), '.', -1)
, SUBSTRING_INDEX(SUBSTRING_INDEX(SomeColumn, '.', 2), '.', -1)
, SUBSTRING_INDEX(SUBSTRING_INDEX(SomeColumn, '.', 3), '.', -1)
, SUBSTRING_INDEX(SUBSTRING_INDEX(SomeColumn, '.', 4), '.', -1)
, SUBSTRING_INDEX(SUBSTRING_INDEX(SomeColumn, '.', 5), '.', -1)
FROM (
SELECT GROUP_CONCAT(...) AS SomeColumn
FROM ...
) AS SomeTable
I have two tables and i need to get list of all store_ids that are not in the other table
BusinessUnit Table User Table
StoreId(varchar) StoreId(varchar)
1 1,2
2 3,4
3 1,5
4 4,6
7 4
How to get values of storeid 5,6 which are not present in the business unit table but are present in the user Table? Tried to use several using find_in_set and nothing works.
Use SUBSTRING_INDEX to get all the values from the CSV field. Since there can be up to 6 IDs in the CSV, you need to call it once for each position.
SELECT u.StoreId
FROM (
select substring_index(StoreId, ',', 1) AS StoreID
FROM User
UNION
select substring_index(substring_index(StoreId, ',', 2), ',', -1)
FROM User
UNION
select substring_index(substring_index(StoreId, ',', 3), ',', -1)
FROM User
UNION
select substring_index(substring_index(StoreId, ',', 4), ',', -1)
FROM User
UNION
select substring_index(substring_index(StoreId, ',', 5), ',', -1)
FROM User
UNION
select substring_index(substring_index(StoreId, ',', 6), ',', -1)
FROM User) AS u
LEFT JOIN BusinessUnit AS b ON u.StoreId = b.StoreID
WHERE b.StoreId IS NULL
DEMO
IF you know all the possible values (and the number of them is reasonably manageable) you can populate a new table with them (you can make it TEMPORARY or just DROP it afterwards), and do this
SELECT *
FROM (
SELECT allIDs.Id
FROM allIDs
INNER JOIN `User` AS u
-- ON CONCAT(',', u.StoreID, ',') LIKE CONCAT('%,', allIDs.Id, ',%')
ON FIND_IN_SET(allIDs.Id, u.StoreID)
) AS IDsInUserTable
LEFT JOIN `BusinessUnit` AS b ON IDsInUserTable.Id = b.StoreID
HAVING b.StoreID IS NULL
;
In this example, allIDs is the aforementioned "possible values" table.
I have the following code
SELECT
GROUP_CONCAT(
CONCAT('{"id"', id, ','),
CONCAT('"COL1"', col1, ','),
CONCAT('"col2"', col2, ','),
CONCAT('"col3"', col3, ','),
CONCAT('"col4"', col4, '}')
SEPERATOR '\n')
AS json from tableX
The whole group_concat returns null. If I removed one concat (at random) it works.
The table contains about 15 million rows.
I have set
SET SESSION group_concat_max_len = 188446744073709551615;
Any ideas why it return null?
Try this, handling any null values and seeing if your output is indeed exceeding the length threshold:
select json, length(json) from (
SELECT
GROUP_CONCAT(
CONCAT('{"id"', coalesce(id, '""'), ','),
CONCAT('"COL1"', coalesce(col1, '""'), ','),
CONCAT('"col2"', coalesce(col2, '""'), ','),
CONCAT('"col3"', coalesce(col3, '""'), ','),
CONCAT('"col4"', coalesce(col4, '""'), '}')
SEPARATOR '\n')
AS json from tableX
) x
-- limit 200000
If the group_concat function lets NULL values override everything else (I'm not sure about that), then you have it catered for.
UPDATE: SEPERATOR was a typo - it should be SEPARATOR.