Mysql 从一个表中检索数据,同时比较另一个表中的值

Mysql 从一个表中检索数据,同时比较另一个表中的值,mysql,sql,Mysql,Sql,概述 我有两张表,如下所示: user_planes ---------------------------------- |id |user_id|plane_id|fuel|status| ---------------------------------- | 2 1 1 1 Ready | ---------------------------------- shop_planes ------------------------ |id |name|

概述

我有两张表,如下所示:

user_planes
----------------------------------
|id |user_id|plane_id|fuel|status|
----------------------------------
| 2     1        1     1   Ready |
----------------------------------

shop_planes
------------------------
|id |name|fuel_capacity|
------------------------
| 1   bob       3      |
------------------------

Foreign Key              Primary Key
user_planes.plane_id <-> shop_planes.id 

SELECT*FROM`user\u planes`内部连接`shop\u planes`到user\u planes.fuel
  • 我已经搜索了Stackoverflow,并查看了许多问题,但我还没有找到答案
  • 我查阅了很多教程,但仍然没有得到理想的结果
期望的结果是,查询应使用存储在
用户平面
中的数据从
车间平面
检索数据,同时不从
车间平面
获取任何多余数据

SELECT * FROM `user_planes` WHERE fuel IN (SELECT shop_planes.fuel_capacity FROM shop_planes WHERE fuel < shop_planes.fuel_capacity) AND user_planes.user_id = 1 AND status = 'Ready'
免责声明

我真的很难使用
JOIN
查询,我可以使用多个单独的查询,但是我希望优化我的查询,因此我尝试将其引入到一个查询中

如果他们的问题不清楚,请说,我会尽我所能更新它

注意-是否有通过phpmyadmin或其他软件提供的简易查询生成器选项


提前感谢。

您上次的尝试还不错,唯一错过的是您在文章开头描述的加入标准。我还将其他过滤器移动到
where
子句,以便更好地区分连接条件和过滤器

SELECT `user_planes`.*
FROM `user_planes`
INNER JOIN `shop_planes` ON user_planes.plane_id = shop_planes.id 
WHERE user_planes.fuel < shop_planes.fuel_capacity AND user_planes.user_id = 1 AND user_planes.status = 'Ready'
选择“用户平面”*
来自“用户”平面`
用户\u planes.plane\u id=shop\u planes.id上的内部联接'shop\u planes'
其中user_planes.fuel
首先,您需要基本的
加入

  SELECT up.*  -- only user_plane fields
  FROM shop_planes sp  -- CREATE alias for table or field
  JOIN user_planes up
    ON sp.id  = up.plane_id 
案例1:使用php参数在where条件中应用过滤器

  SELECT up.*
  FROM shop_planes sp
  JOIN user_planes up
    ON sp.id  = up.plane_id 
  WHERE up.user_id = ?
案例2:使用字符串常量在where条件下应用过滤器

  SELECT up.*
  FROM shop_planes sp
  JOIN user_planes up
    ON sp.id  = up.plane_id 
  WHERE user_planes.status = 'Ready'
案例3:aply筛选器比较两个表中的字段

  SELECT up.*
  FROM shop_planes sp
  JOIN user_planes up
    ON sp.id  = up.plane_id
  WHERE up.fuel < sp.fuel_capacity
选择up*
来自shop_planes sp
将用户连接起来
ON sp.id=上平面\U id
其中up.fuel
尝试以下方法:

SELECT 
      up.id AS User_Plane_ID
    , up.[user_id]
    , up.plane_id
    , up.fuel
    , up.[status]
    , sp.name AS shop_Plane_Name
    , sp.fuel_capacity AS shop_Plane_Fuel_Capacity
FROM User_Planes up
INNER JOIN Shop_Planes sp ON up.plane_id = sp.id  
    AND up.fuel < sp.Fuel_Capacity
WHERE up.[status] = 'Ready'
    AND up.[user_id] = ?
选择
up.id作为用户\u平面\u id
,向上。[用户id]
,up.plane\u id
,加油
,向上。[状态]
,sp.name作为车间\平面\名称
,sp.燃料容量与车间飞机燃料容量相同
从用户_平面向上
内部连接车间\u平面sp在上。平面\u id=sp.id
和up.fuel
一定要找到连接的教程,不要使用SELECT*。使用SELECT*,您可能会查询比实际需要多得多的内容,如果表发生更改,可能会导致问题。如果您在查询中显式地命名所需的列,您将享受更多的一天


我用AS给一些列加了别名,因为其中一些列名可能是保留字。我还移动了加入标准,以包括fuelWell上的过滤器。您需要学习使用
JOIN
,只需遵循基本规则。没有查询生成器,我只是修复了it@JuanCarlosOropeza我试过W3Schools教程,它让我头晕目眩,然后我尝试了一些代码,但仍然无法理解,因此我转向Stackoverflow。注意,select*返回“额外列”,select*告诉查询返回所有内容,当您将两个表连接在一起时,实际上是在生成一个包含两个表中所有列的巨型表。如果您确实想要用户平面中的每一列,那么您可以使用userplanes.*或者最佳做法是单独声明您想要的列,这样做的好处是,如果您要说向用户平面添加一个新列,那么从中选择的任何代码都不必处理新列,因为它不会突然启动returned@MarkD 明白了,谢谢你提供的信息。
SELECT 
      up.id AS User_Plane_ID
    , up.[user_id]
    , up.plane_id
    , up.fuel
    , up.[status]
    , sp.name AS shop_Plane_Name
    , sp.fuel_capacity AS shop_Plane_Fuel_Capacity
FROM User_Planes up
INNER JOIN Shop_Planes sp ON up.plane_id = sp.id  
    AND up.fuel < sp.Fuel_Capacity
WHERE up.[status] = 'Ready'
    AND up.[user_id] = ?