如何将sql的两个结果组合到一个表中?



我有两个工作sql查询:

SELECT first_name, last_name 
FROM customer  
WHERE customer.customer_id = ( 
SELECT customer_id FROM rental  
WHERE return_date IS NULL  
ORDER BY rental_date ASC LIMIT 1 );

SELECT rental_date
FROM rental 
WHERE return_date IS NULL 
ORDER BY rental_date ASC LIMIT 1;

它们都返回一行结果。我的问题是,我怎样才能把这两种结果结合起来,如下所示:

first_name | last_name | rental_dateAaa BBB 2022-5-25

看起来customer_id是租赁表中的外键。所以你可以应用join

SELECT C.first_name, C.last_name , R.rental_date
FROM customer AS C 
INNER JOIN rental AS R ON  C.customer_id = R.customer_id 
WHERE R.return_date IS NULL
ORDER BY R.rental_date ASC LIMIT 1 

或者对于所有的名,姓

SELECT first_name,  last_name ,  
(SELECT  MIN(job_date) FROM rental)AS min_job_date, 
FROM customer  
GROUP BY 1,2;

最新更新