Before I have asked the same problem (Join table with comma issue (MySQL)) about join table with comma in the column.
I have two tables, table structure like below:
First Table name: student
id | name | course_id
——————————————————————————
1 David 1,3
2 Peter 2,4
3 Shawn 2,6
Second Table name: subject
id | subject
———————————————————
1 English
2 Maths
3 Science
4 Geographic
5 Accounting
6 Art & Design
I have tried this find_in_set method (Search with comma-separated value mysql), but it cannot get the actual result. I want the actual result is like below:
id | name | subject_name
——————————————————————————
1 David English,Science
2 Peter Maths,Geographic
3 Shawn Maths,Art & Design
I am using below code:
SELECT student.id as id,student.name as name,student.subject as subject_name
FROM student
INNER JOIN subject
ON FIND_IN_SET(subject.id, student.course_id) > 0
But the result is shown me like below:
id | name | subject_name
——————————————————————————
1 David English
2 David Science
3 Peter Maths
4 Peter Geographic
5 Shawn Maths
6 Shawn Art & Design
Hope someone guide me on how to solve this problem. Thanks.
Like this
SELECT student.id as id, student.name as name, GROUP_CONCAT(subject.subject) as subject_name
FROM student
INNER JOIN subject
ON FIND_IN_SET(subject.id, student.course_id) > 0
GROUP BY student.id, student.name
Usually we don't concat everything in SQL query, but you can do
SELECT CONCAT_WS(' ', student.id, student.name, GROUP_CONCAT(subject.subject)) as concated_value
FROM student
INNER JOIN subject
ON FIND_IN_SET(subject.id, student.course_id) > 0
GROUP BY student.id, student.name
Related
Hi everybody I need done help with this issue.
Example myDb tabele name people
ID name Sex
3 Adam Smith M
5 Sonia Kolan F
3 Donald Smith M
Select id, name from people where name LIKE ‚%Smith%’;
Result
Id Name
3 Adam Smith
3 Donald smith
My question is how to transform my result of col name to this view
Id name
3 Smith
3 Smith
I want too see in col name Expresion from like statement
I want too see in col name Expresion from like statement
Then just put that literal string in the select clause:
select id, 'Smith' name
from people
where name like '%Smith%'
You can join to the table a query that returns only 'Smith':
select p.id, t.name
from people p
inner join (select 'Smith' name) t
on p.name LIKE concat('%', t.name, '%');
See the demo.
Results:
> id | name
> -: | :----
> 3 | Smith
> 3 | Smith
table 1
uid email name password
----------------------------------------------
1 abc#abc.com John Doe 9q8wekdshfa
2 xyc#xyc.com Jane Doe a42adsflda2
3 me#meme.com Meme Me asd4q23llsd
table 2
id uid vocation groups
-----------------------------------
1 1 Programmer 1,3,4,5
2 2 Designer 2,4,6,8
3 3 Attorney 3
How do I write the query to get all the data about the user/s that belongs to the same group as the active logged in person. Suppose that I'm logged in as the 3rd account (me#meme.com) and I already have the data that my group is 3. How do I write the query to get this result?
uid email name password vocation groups
----------------------------------------------------------------------
1 abc#abc.com John Doe 9q8wekdshfa Programmer 1,3,4,5
You just need a basic join statement:
SELECT table1.uid, table1.email, table1.name, table1.password,
table2.vocation, table2.groups FROM table1 INNER JOIN table2 ON
table1.uid = table2.uid
WHERE table2.groups LIKE '%3%' AND email<>'me#meme.com'
And certainly read up on JOIN and basic SQL table relationships. This is database 101 stuff, that's the only reason you're getting a bit of grief in the comments and down votes.
I have two tables.
I am a total newbie to SQL. Using mysql at the moment.
I have the following setup for a school-related db:
Table A contains students records.
Student's id, password,name, lastname and so on.
Table B contains class attendancy records.
Each record goes like this: date, student id, grade
I need to gather all the student info of students that attended classes in a certain date range.
Now, the stupid way would be
first I SELECT all classes from Table B with DATE IN BETWEEN the range
then for each class, I get the student id and SELECT * FROM students WHERE id = student id
What I can't wrap my mind around is the smart way.
How to do this in one query only.
I am failing at understanding the concepts of JOIN, UNION and so on...
my best guess so far is
SELECT students.id, students.name, students.lastname
FROM students, classes
WHERE classes.date BETWEEN 20140101 AND 20150101
AND
classes.studentid = students.id
but is this the appropriate way for this case?
Dont add the join statement in the where clause. Do it like this:
SELECT s.id, s.name, s.lastname,c.date,c.grade
FROM classes c
inner join students s
on c.studentid=s.id
WHERE c.date BETWEEN '01/01/2014' AND '01/01/2015'
This sounds like an assignment so I will attempt to describe the problem and give a hint to the solution.
An example of a union would be;
SELECT students.name, students.lastname
FROM students
WHERE students.lastname IS NOT NULL
UNION
SELECT students.name, 'N/A'
FROM students
WHERE students.lastname IS NULL;
+--------------+--------------+
| name | lastname |
+--------------+--------------+
| John | Doe | <- First two rows came from first query
| Jill | Smith |
| Bill | N/A | <- This came from the second query
+--------------+--------------+
The usual use case for a union is to display the same columns, but munge the data in a different way - otherwise you can usually achieve similar results through a WHERE clause.
An example of a join would be;
SELECT authors.id, authors.name, books.title
FROM authors LEFT JOIN books ON authors.id = books.authors_id
+--------------+--------------+------------------+
| id | name | title |
+--------------+--------------+------------------+
| 1 | Mark Twain | Huckleberry Fin. |
| 2 | Terry Prat.. | Good Omens |
+--------------+--------------+------------------+
^ First two columns from ^ Third column appended
from authors table from books table linked
by "author id"
Think of a join as appending columns to your results, a union is appending rows with the same columns.
In your situation we can rule out a union as you don't want to append more student rows, you want class and student information side by side.
I have a table like
Name Spouse
---------------
John Smitha
Bob Neetha
Neetha Bob
Mona Jack
Smitha John
Jack Mona
and I want results as below using joins in MySQL.
Name Spouse
---------------
John Smitha
Bob Neetha
Mona Jack
(i.e. the couple should be selected only once)
Assuming that you have a IsPrimary field.
You could easily achieve this with the following query.
SELECT P.NAME, S.NAME As SpouseName
FROM PEOPLE P
LEFT JOIN PEOPLE S ON P.Spouse = S.Spouse -- You should have a PK/FK relasionship here (SouseId) and not join on a name string
WHERE P.IsPrimary = 1
a way to do it like that
SELECT `Name`, `Spouse`
FROM Table1
WHERE `Name` <= `Spouse`
demo
I have 3 tables with the following structure:
**users**
id
first_name
last_name
**specialties**
specialty_id
specialty_name
**user_specialties**
user_id
specialty_id
Here is some sample data:
**users**
1 Bill Smith
2 Tom Jones
3 Jill Hayes
**specialties**
1 word
2 web
3 database
**user_specialties**
1 1
2 1
2 3
3 2
3 3
I need to query the data so the specialties are concatinated on one row like the below output
**Desired Result**
Bill Smith word
Tom Jones word,database
Jill Hayes web,database
I am using the following query
SELECT
users.first_name,
users.last_name,
GROUP_CONCAT(specialties.specialtyname)
FROM
users
LEFT JOIN user_specialties ON user_specialties.user_id = users.userid
RIGHT JOIN specialties ON user_specialties.specialty_id = specialties.specialty_id
It is not working...
You're missing a GROUP BY clause. Most likely it should be GROUP BY users.id, and it'd go AFTER the JOIN lines.
I just tested this query
SELECT first_name,last_name,group_concat(specialty_name)
FROM user_specialties map
INNER JOIN specialties skill on user.id = map.user_id
INNER JOIN users user ON skill.specialty_id = map.specialty_id
GROUP BY user.id
Cheers! :-)