This question already has answers here:
Is storing a delimited list in a database column really that bad?
(10 answers)
Closed 4 years ago.
Table name: student:
id name topics
--- ---- --------
1 Test1 1,2,10,15,25
2 Test2 5,21,11,18,13
3 Test3 2,1,16,25,10
4 Test4 2
My query:
select * from student where topics like '%2%'
output: all 4 records.
Expected: But i need to get only 3 record since that id 1,3,4 topics column contains 2. 2nd record doesn't contain 2 .
You can use the function FIND_IN_SET :
select * from student where FIND_IN_SET ('2', topics)
The second record DOES contain 2.
You need to use something like this.
WHere topics=2 or topics like '2,%'or topics like '%,2' or topics like '%,2,%'
The first checks if topics is 2. THe second if 2 is the first, but there are others. THe third checks if 2 is the last, but there are some numbers before, and the forth checks if 2 is somewhere in between
Something like that might help?
SELECT * FROM student WHERE topics LIKE '2,%' OR topics LIKE '%,2,%' OR topics LIKE '%,2' OR topics= '2';
its sad that you cant use IN with LIKE.
Related
This question already has answers here:
Is storing a delimited list in a database column really that bad?
(10 answers)
Closed 2 years ago.
genreTable:
id
genre
1
Pop
2
Rock
3
Electro
songTable:
id
name
genre
1
Song1
1
2
Song2
1,2
3
Song3
2,3
Problem: Lets say I want to build query like:
SELECT * FROM songTable WHERE genre = '1'
It'll only return Song1
But how Do I make sure it also returns Song1, Song2
Any other suggestions regarding re-structuring the table is also accepted...
You should fix your data model! There are many reasons why your data model is broken:
Columns should only contain one value.
Numbers should be stored as numbers, not strings.
Foreign key relationships should be properly declared.
SQL has pretty bad string processing capabilities.
Sometimes, you are stuck with other people's really, really, really bad design decisions. In that case, you can use find_in_set():
select s.*
from songTable s
where find_in_set('1', genre) > 0
This question already has answers here:
Is storing a delimited list in a database column really that bad?
(10 answers)
Closed 4 years ago.
What I have with me?
A string with comma separated :
$str_states = "1,2";
I have a table with the following:
id event_name states
1 ABC 1,4,5
2 PQR 1,2,3
3 XYZ 3,4,5
What I want:
id event_name states
1 ABC 1
2 PQR 1,2
What I tried with a query:
SELECT id,event_name FROM events_table WHERE
FIND_IN_SET('1,2', states);
SELECT id,event_name FROM events_table WHERE
states IN ('51,58');
You can't really use FIND_IN_SET the way you want here. FIND_IN_SET searches for a single value against a CSV string. So, you would have to use OR here:
SELECT id, event_name
FROM events_table
WHERE FIND_IN_SET('1', inLocationId) > 0 OR FIND_IN_SET('2', inLocationId) > 0;
If your starting point for the input is 1,2, then you will have to tease apart the individual values in your app layer.
By the way, storing CSV unnormalized data in your SQL tables is bad practice. You would be better off to fix your design than to use my answer.
As a bonus, here is a more terse way to write your query using REGEXP:
SELECT id, event_name
FROM events_table
WHERE inLocationid REGEXP '[[:<:]](1|2)[[:>:]]';
But again, please fix your data model.
This question already has answers here:
Find total number of results in mySQL query with offset+limit
(7 answers)
Closed 6 years ago.
I am making a pagination function, here is my case:
1 table (example)
id | title | date | details
Now, I want to retrieve two different results from this table (example)
Count all of the rows (for the total count of all the lists)
I will only show every 10 list per page.
My current code is, I have 2 separated queries for 1 and 2, so it is like 2 connections, my question is, can this be done with a single query and then retrieve both of 1 and 2 results? If so, what do I need to do? Any suggestion/s can help me!
I think,
This will help you.
Step 1: Get the all list from the table
Step 2: Then count the records
Here is the single query to perform it.
SELECT COUNT(tmp.id) as cnt, tmp.* FROM (SELECT id, title, date, details FROM tablename) tmp
I have the following table:
surveys
comp_id question
4 What is your name?
4 How are you?
4 Where do you live?
5 Who are you?
5 What is your birthday?
I need help writing a Query that gives me the following output:
comp_id my_questions
4 What is your name?How are you?Where do you live?
5 Who are you?What is your birthday?
Thanks,
You are looking for the GROUP_CONCAT() function. Use it like this:
SELECT comp_id, GROUP_CONCAT([DISTINCT] question [ORDER BY some_field ASC/DESC] [SEPARATOR '']) as my_questions
FROM surveys
GROUP BY comp_id
Note I have shown some some optional values to pass into GROUP_CONCAT in []. To get exact like you are showing just use GROUP_CONCAT(question SEPARATOR ''). The optional items let you look for distinct question values or order them by any field(s) (including question itself).
Anyone could tell me how to connect these 2 tables. I tried do it myself.. but just wasted a time, i know it's easy but I somehow can't understand it. Table is from my previous question
Table Articles:
ID Content
1 bla
2 blah
3 etc.
4 whatever
Table Similar:
ID Similar_ID
3 1
3 2
4 1
4 2
4 3
select a.ID,a.Content,s.Similar_ID from
Articles a inner join Similar s
on a.ID=s.ID
You want to browse the Similar table, and "convert" its IDs (e.g. 3) in Content (e.g. "Blah").
So:
SELECT * FROM Similar;
will list all similarities. Since we have two Ids to convert (something is similar to something else), we need two separate JOINS with the same table Articles, and we'll alias them to "a" and "b":
SELECT a.Content, b.Content
FROM Similar
JOIN Articles AS a ON (Similar.ID = a.ID)
JOIN Articles AS b ON (Similar.Similar_ID = b.ID)
;
The first JOIN "decodes" the ID field of Similar, the second decodes "Similar_ID".
So
3 1
becomes now
Etc. Blah
Or you can write:
SELECT CONCAT(a.Content, ' is similar to ', b.Content)
FROM... (same query as above)
and get
Etc. is similar to Blah
Blah is similar to whatever
...