MySQL在一个查询中使用多个联接获取结果?

时间:2018-02-19 07:01:19

标签: mysql join

我有以下表格

tbl_user

uid     first_name      last_name       email_id
1       steve           martin          steve1@gmail.com
2       mark            lee             mark1@gmail.com
3       nelson          wise            nelson23@gmail.com

tbl_tier

tier_id     tier_name       points_required
1           Silver          100
2           Gold            200
3           Platinum            300

tbl_tier_earned

id      tier_id     uid
1       1           1
2       2           1
3       3           1
4       1           2
5       2           2
6       1           3

我需要使用当前级别的唯一用户:

first_name          last_name       email_id                    current_tier
steve               martin          steve1@gmail.com            Platinum
mark                lee             mark1@gmail.com             Gold

我尝试过以下查询,但它只给出了1个结果:

SELECT u.first_name,u.last_name,u.email_id, t.tier_name 
FROM tbl_tier_earned AS tte 
INNER JOIN tbl_user AS u 
ON u.uid = tte.uid 
INNER JOIN tbl_tier AS t 
ON tte.tier_id = t.tier_id 
WHERE u.email_id!="" 
ORDER BY t.points_required DESC LIMIT 0,1

如何使用mysql查询检索上述数据?

2 个答案:

答案 0 :(得分:0)

对于每个用户,似乎当前层由tier_id表中的最大tbl_tier_earned给出。这里的一种方法是将用户表连接到tbl_tier_earned表上的子查询,该子查询找到最大层。

SELECT
    u.first_name,
    u.last_name,
    u.email_id,
    COALESCE(t2.tier_name, 'NA') AS current_tier
FROM tbl_user u
LEFT JOIN
(
    SELECT uid, MAX(tier_id) AS max_tier_id
    FROM tbl_tier_earned
    GROUP BY uid
) t1
    ON u.uid = t1.uid
LEFT JOIN tbl_tier t2
    ON t1.max_tier_id = t2.tier_id;

答案 1 :(得分:0)

SQL Fiddle

MySQL 5.6架构设置

CREATE TABLE tbl_user
    (`uid` int, `first_name` varchar(6), `last_name` varchar(6), `email_id` varchar(18))
;

INSERT INTO tbl_user
    (`uid`, `first_name`, `last_name`, `email_id`)
VALUES
    (1, 'steve', 'martin', 'steve1@gmail.com'),
    (2, 'mark', 'lee', 'mark1@gmail.com'),
    (3, 'nelson', 'wise', 'nelson23@gmail.com')
;


CREATE TABLE tbl_tier
    (`tier_id` int, `tier_name` varchar(8), `points_required` int)
;

INSERT INTO tbl_tier
    (`tier_id`, `tier_name`, `points_required`)
VALUES
    (1, 'Silver', 100),
    (2, 'Gold', 200),
    (3, 'Platinum', 300)
;


CREATE TABLE tbl_tier_earned
    (`id` int, `tier_id` int, `uid` int)
;

INSERT INTO tbl_tier_earned
    (`id`, `tier_id`, `uid`)
VALUES
    (1, 1, 1),
    (2, 2, 1),
    (3, 3, 1),
    (4, 1, 2),
    (5, 2, 2),
    (6, 1, 3)
;

查询1

SELECT c.first_name, c.last_name, c.email_id,
(SELECT tier_name from tbl_tier WHERE points_required = max(b.points_required)) as current_tier 
FROM tbl_tier_earned a
INNER JOIN tbl_tier b ON a.tier_id = b.tier_id
INNER JOIN tbl_user c ON a.uid = c.uid
GROUP BY c.first_name, c.last_name, c.email_id

<强> Results

    | first_name | last_name |           email_id | current_tier |
    |------------|-----------|--------------------|--------------|
    |       mark |       lee |    mark1@gmail.com |         Gold |
    |     nelson |      wise | nelson23@gmail.com |       Silver |
    |      steve |    martin |   steve1@gmail.com |     Platinum |