MySQL Order By in Right Join子句

MySQL Order By in Right Join子句,mysql,sql,join,sql-order-by,Mysql,Sql,Join,Sql Order By,我正在尝试在MySQL中进行正确的连接,如下所示: SELECT customers.id,customers.firstname,customers.lastname,customers.email,orders.time,orders.notes,pendings.date_updated,pendings.issue,appointments.closed,appointments.job_description,backup_plans.expiration FROM customers

我正在尝试在MySQL中进行正确的连接,如下所示:

SELECT customers.id,customers.firstname,customers.lastname,customers.email,orders.time,orders.notes,pendings.date_updated,pendings.issue,appointments.closed,appointments.job_description,backup_plans.expiration FROM customers
RIGHT JOIN orders
ON customers.id = orders.customer_id
ORDER BY orders.time DESC LIMIT 1
RIGHT JOIN pendings
ON customers.id = pendings.customer_id
ORDER BY pendings.date_updated DESC LIMIT 1
RIGHT JOIN appointments
ON customers.id = appointments.customer_id
ORDER BY appointments.closed DESC LIMIT 1
RIGHT JOIN backup_plans
ON customers.id = backup_plans.customer_id
ORDER BY backup_plans.expiration DESC LIMIT 1
我的意图是:选择客户的姓名和电子邮件,以及最近的订单、待定、约会和备份计划。执行此操作时,会出现语法错误:

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'RIGHT JOIN pendings
ON customers.id = pendings.customer_id
ORDER BY pendings.d' at line 5
我不熟悉joins,希望您能提供帮助


编辑1:

似乎我需要根据DanK的建议进行子查询,如下所示:

SELECT customers.id,customers.firstname,customers.lastname,customers.email,orderstmp.time,orderstmp.notes FROM customers
RIGHT JOIN (
    SELECT orders.time,orders.notes,orders.customer_id FROM orders ORDER BY orders.time DESC LIMIT 1
) as orderstmp ON orderstmp.customer_id = customers.id
但是当我这样做时,我只得到一行结果,而我想要所有的客户信息


编辑2:

根据Tom H的建议,我构建了以下查询:

SELECT
    customers.id,
    SQ_O.time,
    SQ_O.notes
FROM customers
LEFT JOIN (
    SELECT
        customers.id,
        orders.time,
        orders.notes
    FROM customers
    LEFT JOIN orders ON orders.customer_id = customers.id
    ORDER BY orders.time DESC LIMIT 1
) AS SQ_O ON SQ_O.id = customers.id
其中包含所有空白时间和备注字段

达到最大执行时间。我猜这是因为我不熟悉MySQL与其他方言相比可能的功能

我还尝试了相关子查询,如下所示:

SELECT
    customers.firstname,
    customers.lastname,
    customers.email,
    (
        SELECT CONCAT(orders.time,': ',orders.notes)
        FROM orders
        WHERE orders.customer_id = customers.id
        ORDER BY orders.time DESC LIMIT 1
    ) as last_order
FROM customers
SELECT --fields,    
FROM customers
RIGHT JOIN orders ON customers.id = orders.customer_id
RIGHT JOIN pendings ON customers.id = pendings.customer_id
RIGHT JOIN appointments ON customers.id = appointments.customer_id
RIGHT JOIN backup_plans ON customers.id = backup_plans.customer_id
ORDER BY orders.time DESC, pendings.date_updated DESC, appointments.closed DESC, backup_plans.expiration DESC
LIMIT 1
但是“最后的订单”一栏是空白的


最终,令人失望的编辑


在尝试了一些非常好的建议,帮助我学习SQL之后,我决定写一个PHP脚本来满足我的需求。这个项目的最后期限有点短,所以不管怎样都行。谢谢大家

每个查询只能有一个
ORDER BY
语句。当然,您可以使用子查询并将结果集引用为虚拟表,但最终在单个
SELECT
中,您只能有一个
orderby

例如:

SELECT something
FROM table
ORDER BY something -- One order By
将子查询作为虚拟表:

SELECT something
FROM (SELECT anotherthing, something
      FROM table
      ORDER BY anotherthing) -- this is an order by in a separate select statement..
ORDER BY something -- still only one Order by
------编辑--------

要获得连接语法方面的帮助,请尝试以下操作:

SELECT
    customers.firstname,
    customers.lastname,
    customers.email,
    (
        SELECT CONCAT(orders.time,': ',orders.notes)
        FROM orders
        WHERE orders.customer_id = customers.id
        ORDER BY orders.time DESC LIMIT 1
    ) as last_order
FROM customers
SELECT --fields,    
FROM customers
RIGHT JOIN orders ON customers.id = orders.customer_id
RIGHT JOIN pendings ON customers.id = pendings.customer_id
RIGHT JOIN appointments ON customers.id = appointments.customer_id
RIGHT JOIN backup_plans ON customers.id = backup_plans.customer_id
ORDER BY orders.time DESC, pendings.date_updated DESC, appointments.closed DESC, backup_plans.expiration DESC
LIMIT 1
试试这个:

SELECT customers.id,customers.firstname,customers.lastname,customers.email,orders.time,orders.notes,pendings.date_updated,pendings.issue,appointments.closed,appointments.job_description,backup_plans.expiration FROM customers
RIGHT JOIN orders
ON customers.id = orders.customer_id
RIGHT JOIN pendings
ON customers.id = pendings.customer_id
RIGHT JOIN appointments
ON customers.id = appointments.customer_id
RIGHT JOIN backup_plans
ON customers.id = backup_plans.customer_id
ORDER BY orders.time DESC, pendings.date_updated DESC,  appointments.closed DESC,  backup_plans.expiration DESC LIMIT 1

您可以通过子查询或使用附加的
JOIN
s来实现这一点。这里有一个例子。(注意:我使用SQL Server,所以我习惯的一些语法可能在MySQL中不受相同的支持)。我只是对订单做了这些示例,但希望您能将这些想法扩展到其他表格

使用子查询:

SELECT
    C.id,
    SQ_O.time,
    SQ_O.notes
FROM
    Customers C
LEFT OUTER JOIN
(
    SELECT
        C2.Customer_ID,
        O.time,
        O.notes
    FROM
        Customers C2
    LEFT OUTER JOIN Orders O ON O.customer_id = C2.id
    ORDER BY
        O.time DESC LIMIT 1
) SQ_O ON SQ_O.customer_id = C.id
使用多个
JOIN
s:

SELECT
    C.id,
    O1.time,
    O1.notes
FROM
    Customers C
LEFT OUTER JOIN Orders O1 ON O1.customer_id = C.id
LEFT OUTER JOIN Orders O2 ON O2.customer_id = C.id AND O2.time > O1.time
WHERE
    O2.customer_id IS NULL -- Basically we're excluding any rows where another order was found with a later time than O1

如果Orders.time中可能存在精确匹配,则您需要额外的标准来选择一个。

只要您可以相信没有客户最近的两个
订单
具有相同的
时间
,这应该可以:

SELECT c.firstname, c.lastname, c.email, o.*
FROM customers AS c
LEFT JOIN (
   SELECT customer_id, MAX(`time`) AS maxTime
   FROM orders
   GROUP BY customer_id 
) AS lastO ON c.id = lastO.customer_id
LEFT JOIN orders AS o 
   ON lastO.customer_id = o.customer_id
   AND lastO.maxTime = o.`time`
;

只要其他表也可以依赖于每个客户只有一个
MAX
值,您就应该能够为它们附加类似的
JOIN
s。对于一个客户来说,多个相同的“last”
time\date\u updated\closed\etc.
的问题在于,它们会将结果相乘。例如,在同一客户上,
订单
中相同的
时间
对以及在
待定
中更新的
日期
对将导致4行而不是两行,因为该客户在
订单
中的每一“最后”行都与每一“最后”行配对在
pending

中的行中,每个查询只能有一个ORDER BY语句。#1064-您的SQL语法有错误;查看与您的MySQL服务器版本对应的手册,了解正确的语法,以使用第5行的“right JOIN pendings ON customers.id=pendings.customer_id ORDER BY pendings.d”附近的“right JOIN pendings ON customers.id=pendings;但我还是感到疑惑。为什么一个
加入?您是否希望有
订单
待决
约会
,或
备份计划
记录,而没有相关的客户记录?@uuerdo,因为我是n00b。谢谢你的提示!对于最新编辑,您需要在
电子邮件
之后和
之前使用逗号(选择
;子查询不应加入到客户,它应该有一个
WHERE orders.customer\u id=customers.id
(引用外部查询使其“相关”).Close,但我需要它做的是选择每个客户,
LIMIT
订单、待付款,&实际上,听起来你想做一个相关的子查询..这变得有点复杂..阅读这里的语法:比我的答案好+1 Close,但我需要它做的是选择每个客户,然后
LIMIT
订单s、 pendings和cIf一个用户的订单数大于?@tyler-shuster@tyler-shuster我认为mysql不支持这一点。但我发现UnionThank有一些功能。这看起来非常接近我想要做的,但基于方言我遇到了一些问题。我更新了原始问题。在你的子查询中,你似乎有“customers.id”,但您在子查询之外将其称为“customer_id”。我更改了它,但现在时间和注释字段为空。