is there any way, i can get a field values (comma seperate) into an array or temporary table.
ex: i have field following with values 3,7,23,45
i want get them into an array without using PHP, or into a temporary table.
as i need to do some joint queries based on those values.
any help is appreciated
thanks
my table name is: shoes
field name is following
sample table values are like these
+----------+-----------+
userId following
+----------+-----------+
1 5,7,8,12
2 5,2,1,67
now, when i search for userId 1, i want to get values 5,7,8,12 into an array or temp table.
It is possible although not particularly quick. The best solution is to redesign the database to move the comma separated list into a different table, with one row for each comma separated element.
However if you want an SQL way to do it then something like the following will do it (this relies on having a table called integers with a single column called i, with 10 rows with the values 0 to 9).
SELECT DISTINCT shoes.userId , substring_index(substring_index(shoes.FollowingIdString, ',', anInt), ',', -1) AS SplitField
FROM shoes,
(SELECT a.i+b.i*10+c.i*100 AS anInt
FROM integers a, integers b, integers c) Sub1
HAVING SplitField IS NOT NULL
Assuming you have a table PersonList which has multiple columns and this is the table where you want to join to get the name of the followers list. eg
PersonTable
ID Name
1 AAA
2 BBB
there is a mysql function called FIND_IN_SET, eg
SELECT a.userID, GROUP_CONCAT(b.Name)
FROM Follower a
INNER JOIN Person b
ON FIND_IN_SET(b.id, a.following) > 0
GROUP BY a.userID
SQLFiddle Demo
Related
Apologies if this is answered in SO. I searched and couldn't find an obvious duplicate. I am new to Access and inherited an old Access MDB (2000 File Format). [Note: I am using Office 365.]
Data is in 5 tables. However, only 4 are connected with Relationships. The fifth (unconnected) Table is a mashup of different data in 3 fields: Type, Code, and Description. Type has multiple rows with repeated values. Each Type row has a unique Code and Description. Each unique Type corresponds to a column in Table_1, and the Codes are values found in that column. (There are 3000 rows in Table_5 with over 250 unique Types, each with 1-500+ Codes.)
Here is a simplified version of Table_5:
Type Code Description
Atype A Atype_A_Description
Atype B Atype_B_Description
Atype X1 Atype_X1_Description
...
Class 1 Class_1_Description
Class 2 Class_2_Description
Class 9 Class_9_Description
...
Source A Source_A_Description
Source A1 Source_A1_Description
Source A2 Source_A2_Description
...
To complicate things, there isn't an exact match between the Field names in Table_1 and the Type entries in Table_5. (For example the Atype values in Table_5 correspond to a Field named ACC_TYPE1 in Table_1.)
I'm working on a method to get the Description from Table_5 based on a Field Name and Value from Table_1. I did this for 1 (hardcoded) Type. I created a Query for that Type in Table_5, and connected it to Table_1 with a Relationship.
Here's what I did:
Table_5_Atype_Query (as SQL, let me know if another format is preferred in SO)
SELECT Table_5.[Type], Table_5.[Code], Table_5.[Description]
FROM Table_5
WHERE (((Table_5.[Type])="Atype"));
The Relationship is:
Table/Query: Related Table/Query:
Table_1 Table_5_Atype_Query
ACC_TYPE1 Code
This works perfectly to get the Description field referencing Code from Table_5 based on values of ACC_TYPE1 in Table_1. It is NOT scalable to lookup Descriptions for other Type/Code pairs. (I would need 250 unique queries and relationships.) Put another way, I'd like to create a method to get the Description from Table_5 based on any Column Name and Value from Table_1. Are there better ways to do this?
A UNION query can rearrange fields to a normalized data structure.
UNION query with 5 fields:
SELECT ID, field1 as Code, "f1" as src from table1
UNION SELECT ID, field2, "f2" from table1
UNION SELECT ID, field3, "f3" from table1
UNION SELECT ID, field4, "f4" from table1
UNION SELECT ID, field5, "f5" from table1;
Of course, use your actual field and table names.
Now use that query in another query joining to table5.
There is a limit of 50 SELECT lines. There is no query designer or wizard for UNION - must use SQLView.
I have a column holds a comma-separated values.
1,2,3
4,6,7
2,3,8
12234,5467,232445,232455,11223
With given criteria of array (e.g., 1,4,9),
How can I select rows whose value contains any of given?
I mean when I am given with 1,4,9, I need to select
1,2,3 -- has 1
4,6,7 -- has 4
UPDATE
I have a table who has a column of comma-separated values of other entity's primary keys.
I understand the reason why the original table designer did this. The other entity actually resides in other database which means not joinable. Or he or she just wanted to do like this.
The STUDENT table
id name classes
---------------------------
1 John 1,2,3
2 Jane 2,8,233423423
The Criteria
With given comma-separated class numbers, find students who is attending any of them.
given: 1 -> select John
given: 233423423 -> select Jane
given: 1,233423423 -> select Both
You can use dynamic template for regular expression. For example:
SET #Criteria='1,4,9';
SELECT `name`
FROM STUDENT
WHERE STUDENT.classes REGEXP concat('(^|,)(', REPLACE(#Criteria, ',', '|'), ')(,|$)');
If you have an input 1,4,9 and you have to find rows where any of 1, 4, or 9 occur in a comma-separated list?
SELECT ...
FROM MyTable
WHERE FIND_IN_SET(1, mycolumn)
OR FIND_IN_SET(4, mycolumn)
OR FIND_IN_SET(9, mycolumn)
See https://dev.mysql.com/doc/refman/5.7/en/string-functions.html#function_find-in-set for information about this function.
This should illustrate to you that storing comma-separated lists is not a good idea for a relational database, when you want to treat the elements of the list as discrete values.
See also my answer to Is storing a delimited list in a database column really that bad?
lets take an example - i have 2 data tables, table "books" with columns "shelfId" and "text", and table "shelves" with column "Id". I want to join these two tables on books.shelfId == shelves.Id, and as a result, i want to see a new table with 2 columns - column 1 has unique values of Ids, and column 2 has merged values of books.text with same books.shelfId values and separated by comma or something else, i.e. :
Is it possible to write such sql select to get what i need ?
Here is fiddle http://sqlfiddle.com/#!2/c96dfa/1
SELECT shelfid as id, GROUP_CONCAT(text) AS text
FROM books
GROUP BY shelfid
I have Structure like this table.
ID Category Publisher
1 1,2 A
2 1 B
3 2 C
4 1 D
5 2 E
6 2,3,1 F
I want a query, when i search for category for 1,3 then it's return
following
I tried but don't get any optimized way.
ID Category Publisher
1 1,2 A
2 1 B
4 1 D
6 2,3,1 F
select * from your_table
where find_in_set(1, category) > 0
or find_in_set(3, category) > 0
But actually you should never store multiple values in a single column. Better change your table design.
While pure SQL does not offer the tools to compare between two sets, this can be implemented in code (JavaScript/PHP) or using stored procedures.
In order to use pure SQL, you could implement a stored procedure to return the comma-separated string as a single-column temporary table (this answer and this post can give you an idea for implementing this part).
And then, utilizing what you have created, assuming the function is implemented
STR_SPLIT(<string>, <delimiter>) and returns a table with a single column named colName, you could write:
SELECT DISTINCT `tblName`.*
FROM `tblName`
INNER JOIN STR_SPLIT('1,3',',') `given`
ON FIND_IN_SET(`given`.`colName`, `category`) > 0
This will take each value and match it separately, which will created duplicates that are going to be eliminated by the DISTINCT statement, making this a non-ideal solution.
I'd recommend using a reference table in this case of data, which will have made this issue a breeze to handle, or implementing a partial solution and finishing code-side.
I Have MySQL DB table tbl_users, which has two uid, name, usergroupid. usergroupid contains the comma separated integers (IE: 1,2,3).
I want all the users which contain the user group ids say 1or 2 or 3 or 1,2 or 2,3 or 3,1 or 1,2,3. I am getting the value to compare in database from a variable which itself contains the comma separated values say, 1.2.3 or 1,2 or 2,3 or 3,1 or 1,2,3. Now what would be the select query to fetch the result.
I am using below select query to find the result but its wrong.
SELECT user_id, user_fname, user_lname
FROM tbl_users
WHERE user_group_id IN (".$usergroupid.")
here $usergroupid is the variable which contains the CSVs
The best thing to do in this situation is to use pattern matching to find the required values.
SELECT user_id, user_fname, user_lname FROM tbl_users WHERE user_group_id LIKE (%".$usergroupid."%)
This is if you want the exact pattern in the csv, else you have to break your csv according based on , and run it in a loop to get the desired result.
But i think the best thing for something like this is having another table usergroup with user and group fields which will be a mapping from user to group. So in this case you can just do
SELECT userID FROM usergroup WHERE groupid IN $usergroupid
LIKE has the potential to pick up on completely the wrong usergroup if you do LIKE '%1%' then if you've a group 10, 11, 12 etc. it'll match.
Assuming that all of your strings are of the form x,y,z,... then a less naive LIKE would be more robust.
SELECT user_id, user_fname, user_lname
FROM tbl_users
WHERE (',' + user_group_id + ',') LIKE '%,".$usergroupid.",%'
Have you tried using FIND_IN_SET? I've been using it sometimes and it works pretty well in those cases.
"FIND_IN_SET(user_group_id,".$user_group_id".)"
Some more info: here