Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 选择从两个DB表到一个输出的输出_Mysql_Join - Fatal编程技术网

Mysql 选择从两个DB表到一个输出的输出

Mysql 选择从两个DB表到一个输出的输出,mysql,join,Mysql,Join,我有两张表,操作员(操作飞机)和历史(历史是飞机的运动)。我需要做的是只显示历史表上有“活动”标志的操作符作为最后一个条目。 例如,我在历史表中有10个历史表中飞机的条目。最后一个条目是英国航空公司,该飞机处于活动状态。因此,这应该允许英国航空公司在输出中列出。 然后我有10个参赛的飞机,当时是英国飞,然后被出售给英国航空公司。两者都有或都有飞机处于活动状态,但由于英国航空公司是该飞机当前的活动运营商,因此不应在该列表中显示“飞英国”。 两个运营商可能在列表中都有多架飞机,有些飞机处于活动状态,

我有两张表,操作员(操作飞机)和历史(历史是飞机的运动)。我需要做的是只显示历史表上有“活动”标志的操作符作为最后一个条目。 例如,我在历史表中有10个历史表中飞机的条目。最后一个条目是英国航空公司,该飞机处于活动状态。因此,这应该允许英国航空公司在输出中列出。 然后我有10个参赛的飞机,当时是英国飞,然后被出售给英国航空公司。两者都有或都有飞机处于活动状态,但由于英国航空公司是该飞机当前的活动运营商,因此不应在该列表中显示“飞英国”。 两个运营商可能在列表中都有多架飞机,有些飞机处于活动状态,有些飞机处于订购、存储状态,等等。 表格-

历史 有操作员和状态字段(可激活、存储、报废、注销)

飞机 具有运算符名称的字段

所以应该是这样的

select * 
from operators 
where max history.status = 'active' or 'stored' or 'grounded';
但是它不起作用了

我尝试了各种各样的代码,改变了php版本,在这种情况下没有更好的效果

select * 
from operators 
where max history.status = 'active' or 'stored' or 'grounded';

我没有得到任何与此相关的输出

您可以使用相关子查询:

select o.*
from operators o
where (
    select h.status
    from history h
    where h.operatorName = o.operatorName
    order by h.operationDate desc
    limit 1
) in ('active', 'stored', 'grounded')
在MySQL 8.0中,窗口函数非常方便:

select *
from (
    select 
        o.*, 
        h.status,
        rank() over(partition by h.operatorName order by h.operationDate desc) rn
    from operators
    inner join history h on h.operatorName = o.operatorName
) t
where 
    rn = 1 
    and status in ('active', 'stored', 'grounded')

history.status='active'或'storage'或'grounded'
无法按预期工作。这将始终是
真的

实际上有3个条件语句:
history.status='active'

“存储的”

“接地”

“存储的”
“固定的”
永远不会为false,因为非空字符串不会为false


可以通过这种方式重写您的条件,并保留逻辑:
(history.status='active')或(TRUE)或(TRUE)
,其计算结果为
TRUE
,而与
历史记录的值无关。status

您可以使用相关子查询:

select o.*
from operators o
where (
    select h.status
    from history h
    where h.operatorName = o.operatorName
    order by h.operationDate desc
    limit 1
) in ('active', 'stored', 'grounded')
在MySQL 8.0中,窗口函数非常方便:

select *
from (
    select 
        o.*, 
        h.status,
        rank() over(partition by h.operatorName order by h.operationDate desc) rn
    from operators
    inner join history h on h.operatorName = o.operatorName
) t
where 
    rn = 1 
    and status in ('active', 'stored', 'grounded')

history.status='active'或'storage'或'grounded'
无法按预期工作。这将始终是
真的

实际上有3个条件语句:
history.status='active'

“存储的”

“接地”

“存储的”
“固定的”
永远不会为false,因为非空字符串不会为false


可以通过这种方式重写您的条件,并保留逻辑:
(history.status='active')或(TRUE)或(TRUE)
,其计算结果为
TRUE
,无论
history.status的值如何。status

您错过了表的别名operators@Cid:当然,请继续。如果需要,请随意重新措辞。您错过了表的别名operators@Cid:当然可以,请继续。如果需要,请随意重新措辞