0

I have two table that I need to combine in a specific way. I need the information in one table displayed once but I need the information in the next table to return every matching data it has. here is my example

user


| id | name |
-------------
| 1  | mike |
| 2  | john |
| 3  | bill |
-------------

class

-----------------
| uid | subject | 
-----------------
| 1   | math    |
| 1   | English |
| 2   | math    |
| 3   | math    |
| 3   | English |
-----------------

expected results

--------------------
|name   | mike      |
---------------------
|subject| math      |
|       | English   |
---------------------

or

--------------------
|name   |  subject  |
---------------------
|mike   | math      |
|       | English   |
---------------------

this is my current query

SELECT user.name, class.subject
FROM user
join class on class.uid = user.id
WHERE (user.name like '%mike%')

returns

--------------------
|name   |  subject  |
---------------------
|mike   | math      |
|mike   | English   |
---------------------

but I want to remove all name duplicates. can anyone help with this query

4
  • 1
    Why do you think it should return a blank for the second line. This is not the standard outcome expected of a join. Commented Jan 1, 2013 at 0:29
  • 3
    IMO you would be abusing the database to do this work there. You should order by name and do the grouping in your application code. Commented Jan 1, 2013 at 0:29
  • 1
    SQL doesn't really work that way. If you want to group those in your application output, the presentation should be done there. It can be done with a lot of horrifying string manipulation, but that is more appropriately done in the application layer. Commented Jan 1, 2013 at 0:29
  • GROUP_CONCAT function will assemble a comma-separated string of results with a common group. I think it's what you're looking for. (granted you're not really looking for a common result). Commented Jan 1, 2013 at 0:40

2 Answers 2

2

You could concatenate the values into a delimited string with a join type function... e.g.

Select u.name, (
      select
      GROUP_CONCAT(c.subject) FROM classes c where u.id = c.uid
    ) As subjects
    From user u

so you end up with

--------------------
|name   |  subjects  |
---------------------
|mike   | math, English |
Sign up to request clarification or add additional context in comments.

2 Comments

Aren't you just reinventing GROUP_CONCAT()?
perhaps, just showing the concept, im not big on non-iso mysql stuff
1
set @lastname = null;
select case when name = @lastname
            then ""
            else @lastname := name
       end name,
       subject
from (select name, subject
      from user
      join class on id = uid
      where name like '%mike%'
      order by name) x

FIDDLE

However, as others pointed out in the comments, this is usually done in the application code, not in the SQL. In your loop that processes rows, just check if the name is the same as the previous one, and leave that table field blank. Or use a grid widget that automates grouping for you.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.