简体   繁体   中英

How can I select rows with all Column value, which have the same values?

I am trying to get all the courID values that have same memid values, For example, if my table looked like this (as shown in image):
在此处输入图片说明

SELECT 
  memId, GROUP_CONCAT(courId) 
FROM table_name 
GROUP BY memId

Let assuming you have created you table like shown below

CREATE TABLE test (
  MemId INTEGER,
  CourId VARCHAR (50)
);

and executing insert like shown below

INSERT INTO test VALUES (1, '2');
INSERT INTO test VALUES (1, '3');
INSERT INTO test VALUES (2, '2');
INSERT INTO test VALUES (3, '1');
INSERT INTO test VALUES (3, '3'); 

Run query like so

SELECT MemId, GROUP_CONCAT( CourId) FROM test GROUP BY MemId

Your result should look like this

| MemId | GROUP_CONCAT( CourId) |
|-------|-----------------------|
|     1 |                   2,3 |
|     2 |                     2 |
|     3 |                   1,3 |

for further reading and understanding see this link

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM