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

lokaqttq  于 2021-06-21  发布在  Mysql
关注(0)|答案(4)|浏览(416)

我有两张table如下:
表internetclient(id、全名、位置、电话号码)
表internetclientdetails(incdid,icid,date\u sub,date\u exp,ispaid,profile\u sub)
两个表中的数据如下:client

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

客户端订阅

--------------------------------------------------------------------------
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 我整晚都在做这个。任何帮助都非常感谢。

dgenwo3n

dgenwo3n1#

select c.name as 'client_name',cs.client_id,max(cs.sub_date) as 'date_sub',cs.sub_price from client c ,
client_subscription cs where cs.client_id=c.id group by cs.client_id,cs.sub_price;
vhmi4jdf

vhmi4jdf2#

要获得每个客户端的客户端订阅,可以使用自连接

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

演示
或使用左连接

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

演示
使用更新的数据集更新查询

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

更新的演示

sczxawaw

sczxawaw3#

试试这个

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
wz8daaqr

wz8daaqr4#

嗨,试试下面的样品也许对你有帮助。

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

相关问题