我有两个要求

更新:

我需要做这样的事情:

SELECT poste_nom, ups_type_contrat,
(SELECT  `entpro_date`
FROM ENT_PRO
WHERE entpro_user_id = 2
ORDER BY  `entpro_id` DESC
LIMIT 1) ,
serv_nom,
serv_id_resp,
user_credit_cpf,
user_indice_salarial,
FLOOR( DATEDIFF( CURDATE( ) , user_dateentree ) /365 ) AS dateEntree
FROM USER
INNER JOIN USER_POSTE_SERVICE
ON USER.user_id= USER_POSTE_SERVICE.ups_poste_id
INNER JOIN POSTE
ON USER_POSTE_SERVICE. ups_poste_id = POSTE.poste_id
INNER JOIN SERVICE
ON USER_POSTE_SERVICE.ups_id_serv = SERVICE.serv_id
WHERE user_id = 2
ORDER BY user_nom ASC


是否可以仅在一个请求中收集两个请求?

最佳答案

据我了解,您想将子查询的结果简单合并到主SELECT中,如果可以的话,可以这样进行尝试:

    SELECT poste_nom,
           ups_type_contrat,
           ENT_PRO_RESULT.entpro_date,
           serv_nom,
           serv_id_resp,
           user_credit_cpf,
           user_indice_salarial,
           FLOOR( DATEDIFF( CURDATE( ) , user_dateentree ) /365 ) AS dateEntree
      FROM USER
 LEFT JOIN (SELECT entpro_date,
                   entpro_user_id
              FROM ENT_PRO
          ORDER BY entpro_id DESC
             LIMIT 1) ENT_PRO_RESULT
        ON USER.user_id = ENT_PRO_RESULT.entpro_user_id
INNER JOIN USER_POSTE_SERVICE
        ON USER.user_id = USER_POSTE_SERVICE.ups_poste_id
INNER JOIN POSTE
        ON USER_POSTE_SERVICE.ups_poste_id = POSTE.poste_id
INNER JOIN SERVICE
        ON USER_POSTE_SERVICE.ups_id_serv = SERVICE.serv_id
     WHERE user_id = 2
  ORDER BY user_nom ASC


我加入了:

ON USER.user_id = ENT_PRO_RESULT.entpro_user_id


因此,您只需要指定以下内容:

WHERE user_id = 2


子查询将为LEFT JOIN使用当前行用户ID。

关于mysql - 使用mysql仅在2个请求中,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34852804/

10-10 14:11