MySQL查询根据特定条件从多个表中选择

时间:2018-05-24 04:41:32

标签: mysql select join union greatest-n-per-group

我有两张表如下:

internetclient (ID,FULL_NAME,位置,PHONE_NUMBER)

internetclientdetails (incdid,icid,date_sub,date_exp,isPaid,profile_sub)

两个表中的数据如下: 客户

--------------------------------------------------------
id         full_name       location    phone_number
-------------------------------------------------------
4         Joe Amine         beirut       03776132
5         Mariam zoue       beirut       03556133

client_subscription

--------------------------------------------------------------------------
incdid   icid      date_sub      date_exp      isPaid      sub_price
----------------------------------------------------------------------------
  6        4      2018-01-01     2018-01-30      0           2000
  7        5      2017-01-01     2017-01-30      0           1000
  8        4      2018-03-01     2018-03-30      1           50000
  9        5      2018-05-01     2019-05-30      1           90000

注意:incdid代表internetClientDetailsId 和icid代表internetClientId

问题 我想根据最新的客户订阅日期创建一个返回客户名称和所有详细信息的查询,结果如下:

------------------------------------------------------------
full_name     client_id     date_sub     sub_price
------------------------------------------------------------
 Joe Amine       4           2018-03-01      50000
 Mary            5           2018-05-01      90000

我在干什么

SELECT * FROM client c LEFT JOIN client_subscription c_s on c.id=c_s.client_id UNION SELECT * FROM client c RIGHT JOIN client_subscription c_S on c.id=c_s.client_id WHERE c.sub_date=(SELECT MAX(sub_date) from client_subscription c_s INNER JOIN client c on c.id=c_s.client_id GROUP BY c_s.client_id

我整晚都在努力。任何帮助都很受欢迎。

4 个答案:

答案 0 :(得分:1)

要为每个客户端获取client_subscription,您可以使用自联接

select c.name, a.client_id, a.date_sub, a.sub_price
from client_subscription a
join (
    select client_id, max(date_sub) date_sub
    from client_subscription
    group by client_id
) b on a.client_id = b.client_id and a.date_sub = b.date_sub
join client c on a.client_id = c.id
order by a.date_sub

Demo

或使用左连接

select c.name, a.client_id, a.date_sub, a.sub_price
from client_subscription a
left join client_subscription b on a.client_id = b.client_id and a.date_sub < b.date_sub
join client c on a.client_id = c.id
where b.client_id is null
order by a.date_sub

Demo

使用更新后的数据集更新了查询

select c.full_name, a.icid, a.date_sub, a.sub_price
from internetclientdetails a
join (
    select icid, max(date_sub) date_sub
    from internetclientdetails
    group by icid
) b on a.icid = b.icid and a.date_sub = b.date_sub
join internetclient c on a.icid = c.id
order by a.date_sub;

select c.full_name, a.icid, a.date_sub, a.sub_price
from internetclientdetails a
left join internetclientdetails b on a.icid = b.icid and a.date_sub < b.date_sub
join internetclient c on a.icid = c.id
where b.icid is null
order by a.date_sub

Updated Demo

答案 1 :(得分:1)

您可以尝试以下示例可能对您有所帮助。

DECLARE @tblClient AS TABLE (ID INT , Name varchar(100))
DECLARE @tblClientSub As TABLE (id INT,client_id INT,date_sub DATE,sub_price INT)

INSERT INTO @tblClient (id,Name)
VALUES 
(1,'Linda'),
(2,'Mary'),
(3,'Joe')  

INSERT INTO @tblClientSub(Id,client_id ,    date_sub ,    sub_price)
VALUES
(1,1,'2018/01/01',50),
(2,2,'2018/02/01',50),
(3,2,'2018/03/01',30),
(4,2,'2018/04/01',30),
(5,3,'2018/01/01',50),
(6,3,'2018/07/01',50),
(7,1,'2018/02/01',40)

SELECT c.Id,c.Name,cs.date_sub,cs.sub_price
FROM @tblClient c
CROSS APPLY (SELECT TOP (1)date_sub,sub_price 
             FROM @tblClientSub 
             WHERE client_id = c.Id 
             ORDER BY date_sub DESC) cs

答案 2 :(得分:0)

试试这个

SELECT c.Name, c.id , MAX(date_sub), sub_price  FROM client c LEFT JOIN client_subscription c_s on c.id=c_s.client_id
GROUP BY c.id
ORDER BY c.id ASC 

答案 3 :(得分:0)

  var keys = Object.keys( obj ).reverse();
  for(var i = 0; i < keys.length; i++){
    var key = keys[i];
    var value = obj[key];
    //do stuff backwards
  }