从最小值和特定名称联接表

时间:2018-12-26 12:47:23

标签: sql join

我有:

表ID

+--------+
| number |
+--------+
|      1 |
|      2 |
|      3 |
+--------+

表格数据

+-------+--------------+
| name  | phone_number |
+-------+--------------+
| Bob   |          111 |
| John  |          333 |
| Alice |          555 |
+-------+--------------+

如何将表与结果连接:(最小值的数字和name ='John')?

+--------+-------+--------------+
| number | name  | phone_number |
+--------+-------+--------------+
|      1 | John  |          333 |
+--------+-------+--------------+

3 个答案:

答案 0 :(得分:1)

您可以在下面尝试-

   select
        (select min(number) FROM ID) as number, name, phone_number
    from date 
    where name = 'John'

答案 1 :(得分:0)

根据您所使用的RDBMS,此查询应使您接近。

SELECT 
    MIN_NUMBER, NAME, PHONE_NUMBER
FROM
    DATA LEFT JOIN (SELECT MIN(NUMBER) AS MIN_NUMBER FROM ID) ON 1=1
WHERE NAME = 'JOHN'

答案 2 :(得分:0)

您可以使用cross join

select min(number) as number, name, phone_number
from Table_Id
cross join Table_Data
group by name, phone_number