来自另一个表的MySQL信息

时间:2014-02-01 23:33:41

标签: mysql sql

我有以下SQL:

Select roleid , deity_level FROM default_jd_deity_role LIMIT 10;

这给出了输出:

+--------+-------------+
| roleid | deity_level |
+--------+-------------+
|   1024 |           1 |
|   1043 |          54 |
|   1056 |           1 |
|   1057 |          54 |
|   1072 |          54 |
|   1074 |          45 |
|   1075 |          36 |
|   1088 |          45 |
|   1089 |          45 |
|   1104 |          27 |
+--------+-------------+

然后我有这个SQL:

Select roleid , name FROM default_jd_ingame_roles LIMIT 22, 10

这给出了以下输出:

+--------+---------+
| roleid | name    |
+--------+---------+
|   1024 | Hulu    |
|   1043 | Cookiez |
|   1056 | Sam     |
|   1057 | Sugar   |
|   1072 | Leah    |
|   1073 | Smexy   |
|   1074 | Bam!    |
|   1075 | Lexi    |
|   1088 | OneShot |
|   1089 | Demono  |
+--------+---------+

我想要做的是将deity_level添加到第二个SQL查询,如下所示:

+--------+---------+-------------+
| roleid | name    | deity_level |
+--------+---------+-------------+
|   1024 | Hulu    |           1 |
|   1043 | Cookiez |          54 |
|   1056 | Sam     |           1 |
|   1057 | Sugar   |          54 |
|   1072 | Leah    |          54 |
|   1073 | Smexy   |          45 |
|   1074 | Bam!    |          36 |
|   1075 | Lexi    |          45 |
|   1088 | OneShot |          45 |
|   1089 | Demono  |          27 |
+--------+---------+-------------+

3 个答案:

答案 0 :(得分:1)

试试这个:

Select a.roleid , a.deity_level, b.name
FROM default_jd_deity_role AS a
JOIN default_jd_ingame_roles AS b ON a.roleid=b.roleid
LIMIT 10

答案 1 :(得分:0)

您可以使用JOIN来实现此目的,如下所示:

SELECT main.roleid, roles.name, main.deity_level
FROM default_jd_deity_role main
LEFT JOIN default_jd_ingame_roles roles ON main.roleid = roles.roleid
LIMIT 22, 10

我将留下两个很好的教程,让您开始JOINS

http://www.sitepoint.com/understanding-sql-joins-mysql-database/ http://www.codinghorror.com/blog/2007/10/a-visual-explanation-of-sql-joins.html

答案 2 :(得分:0)

您正在寻找一个JOIN查询:

SELECT igr.roleid , igr.name, dr.diety_level
FROM default_jd_ingame_roles igr
JOIN default_jd_deity_role dr ON dr.roleid = igr.roleid