查询返回单行,其中多个项目位于该行内的单独列中
我遇到的情况是返回多行结果。我正在寻找一种返回单行的方法,其中多个项目位于该行内的单独列中。我的初始查询:
SELECT a.name, a.city, a.address, a.abbrv, b.urltype, b.url
FROM jos__universityTBL as a
LEFT JOIN jos__university_urlTBL as b on b.universityID = a.ID
WHERE a.stateVAL = 'CA'
我的输出:
| University Of Southern Califor | Los Angeles | | usc | 2 | http://web-app.usc.edu/ws/soc/api/ |
| University Of Southern Califor | Los Angeles | | usc | 4 | http://web-app.usc.edu/ws/soc/api/ |
| University Of Southern Califor | Los Angeles | | usc | 1 | www.usc.edu |
| San Jose State University | San Jose | | sjsu | 2 | http://info.sjsu.edu/home/schedules.html |
| San Jose State University | San Jose | | sjsu | 4 | https://cmshr.sjsu.edu/psp/HSJPRDF/EMPLOYEE/HSJPRD/c/COMMUNITY_ACCESS.CLASS_SEARCH.GBL?FolderPath=PORTAL_ROOT_OBJECT.PA_HC_CLASS_SEARCH |
| San Jose State University | San Jose | | sjsu | 1 | www.sjsu.edu
我的表架构...
mysql> describe jos_universityTBL;
+----------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------+--------------+------+-----+---------+----------------+
| name | varchar(50) | NO | UNI | | |
| repos_dir_name | varchar(50) | NO | | | |
| city | varchar(20) | YES | | | |
| stateVAL | varchar(5) | NO | | | |
| address | varchar(50) | NO | | | |
| abbrv | varchar(20) | NO | | | |
| childtbl | varchar(200) | NO | | | |
| userID | int(10) | NO | | 0 | |
| ID | int(10) | NO | PRI | NULL | auto_increment |
+----------------+--------------+------+-----+---------+----------------+
9 rows in set (0.00 sec)
mysql> describe jos_university_urlTBL;
+--------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+--------------+------+-----+---------+----------------+
| universityID | int(10) | NO | | 0 | |
| urltype | int(5) | NO | | 0 | |
| url | varchar(200) | NO | MUL | | |
| actionID | int(5) | YES | | 0 | |
| status | int(5) | YES | | 0 | |
| ID | int(10) | NO | PRI | NULL | auto_increment |
+--------------+--------------+------+-----+---------+----------------+
6 rows in set (0.00 sec)
我真的很想得到类似的东西:
|<<the concated urltype-url >>|
ucs | losangeles | usc.edu | 1-u1 | 2-u2 | 3-u2 |
I have a situation where I return results with multiple rows. I'm looking for a way to return a single row, with the multiple items in separate columns within the row. My initial query:
SELECT a.name, a.city, a.address, a.abbrv, b.urltype, b.url
FROM jos__universityTBL as a
LEFT JOIN jos__university_urlTBL as b on b.universityID = a.ID
WHERE a.stateVAL = 'CA'
My output:
| University Of Southern Califor | Los Angeles | | usc | 2 | http://web-app.usc.edu/ws/soc/api/ |
| University Of Southern Califor | Los Angeles | | usc | 4 | http://web-app.usc.edu/ws/soc/api/ |
| University Of Southern Califor | Los Angeles | | usc | 1 | www.usc.edu |
| San Jose State University | San Jose | | sjsu | 2 | http://info.sjsu.edu/home/schedules.html |
| San Jose State University | San Jose | | sjsu | 4 | https://cmshr.sjsu.edu/psp/HSJPRDF/EMPLOYEE/HSJPRD/c/COMMUNITY_ACCESS.CLASS_SEARCH.GBL?FolderPath=PORTAL_ROOT_OBJECT.PA_HC_CLASS_SEARCH |
| San Jose State University | San Jose | | sjsu | 1 | www.sjsu.edu
My table schema...
mysql> describe jos_universityTBL;
+----------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------+--------------+------+-----+---------+----------------+
| name | varchar(50) | NO | UNI | | |
| repos_dir_name | varchar(50) | NO | | | |
| city | varchar(20) | YES | | | |
| stateVAL | varchar(5) | NO | | | |
| address | varchar(50) | NO | | | |
| abbrv | varchar(20) | NO | | | |
| childtbl | varchar(200) | NO | | | |
| userID | int(10) | NO | | 0 | |
| ID | int(10) | NO | PRI | NULL | auto_increment |
+----------------+--------------+------+-----+---------+----------------+
9 rows in set (0.00 sec)
mysql> describe jos_university_urlTBL;
+--------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+--------------+------+-----+---------+----------------+
| universityID | int(10) | NO | | 0 | |
| urltype | int(5) | NO | | 0 | |
| url | varchar(200) | NO | MUL | | |
| actionID | int(5) | YES | | 0 | |
| status | int(5) | YES | | 0 | |
| ID | int(10) | NO | PRI | NULL | auto_increment |
+--------------+--------------+------+-----+---------+----------------+
6 rows in set (0.00 sec)
I'm really trying to get something like:
|<<the concated urltype-url >>|
ucs | losangeles | usc.edu | 1-u1 | 2-u2 | 3-u2 |
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 group_concat :
在 SQL 中生成动态列很困难;如果可能的话,看看是否可以将其移至客户端。如果没有,您可以在子查询中添加行号,并为每个行号指定其自己的列。这是一个表格略有不同的示例:
这会打印:
You could use group_concat:
Generating dynamic columns is hard in SQL; if at all possible, see if it can be moved to the client side. If not, you can add a row number in a subquery, and give each row number its own colum. Here's an example with slightly different tables:
This prints: