SQL遍历返回结果的最佳方法 [英] SQL best way to iterate through returned results

查看:66
本文介绍了SQL遍历返回结果的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试练习SQL技能,我有这样的模式:

I am trying to practice my SQL skills, I have this schema:

然后我想到了这个查询:

And i came up with this query:

SELECT vehicle_id 
FROM reservation 
WHERE NOT (pickup_date<='2017-05-20' 
AND end_date>='2017-05-15')

它将返回车辆ID的行,我希望能够使用这些车辆ID并从车辆表中返回车辆名称.怎么办呢?我听说过SQL连接会达到JOIN的目的吗?

it will return rows of vehicle_ids, I want to be able to use those vehicle ids and return vehicle_name from vehicle table. How to go about this? I have heard of SQL joins will a JOIN achieve this goal?

推荐答案

我不确定您要查询什么,但是如果要通过查询返回车辆ID和车辆名称,则应执行以下操作:

I am not sure of what you ask for, but if you want one query to return both the vehicle ids and the vehicle names, you should do this :

SELECT r.vehicle_id, v.vehicle_name
FROM reservation r
LEFT JOIN vehicle v ON r.vehicle_id = v.vehicle_id
WHERE NOT (r.pickup_date<='2017-05-20'
AND r.end_date>='2017-05-15')

这将返回两列:车辆ID之一和车辆名称之一.

This will return two columns : one of vehicle ids, and one of vehicle names.

(很抱歉法语单词"vehicule")

(sorry for the french word 'vehicule')

这篇关于SQL遍历返回结果的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆