0

我想知道是否可以在选择或查询后进行连接。这是开始的查询:

    SELECT * 
      FROM citas 
INNER JOIN cita_solicituds 
        ON citas.id=cita_solicituds.cita_id 
INNER JOIN users 
        ON citas.user_id = users.id

正如您所看到的,我正在执行两个连接,基表是citas并且连接到cita_solicituds用户。这是完美地检索结果。但是,在我得到这个结果之后,我想做另一个连接,但使用一个与citas. 它看起来像:

   (SELECT * 
      FROM citas 
INNER JOIN cita_solicituds 
        ON citas.id=cita_solicituds.cita_id 
INNER JOIN users 
        ON citas.user_id = users.id) 
INNER JOIN horario 
        ON (RESULT JUST FETCHED).horario_id = horario.id
4

3 回答 3

2

尝试这个:

SELECT *
FROM
  (SELECT * 
  FROM citas 
  INNER JOIN cita_solicituds ON citas.id=cita_solicituds.cita_id 
  INNER JOIN users ON citas.user_id = users.id) q 
INNER JOIN horario ON q.horario_id = horario.id

我假设您horario_id在或citas中有一个字段。cita_solicitudsusers

于 2013-08-22T17:20:16.477 回答
0

连接几个表是没有问题的,你不需要对它们进行分组,MySQL 会自己理解最佳顺序:

SELECT * 
FROM citas 
INNER JOIN cita_solicituds 
  ON citas.id=cita_solicituds.cita_id 
INNER JOIN users 
  ON citas.user_id = users.id
INNER JOIN horario
  -- not sure if horario_id comes from users or cita_solicituds 
  ON users.horario_id = horario.id 
于 2013-08-22T17:38:21.593 回答
0

它可以像:

SELECT * FROM
  (SELECT * 
  FROM citas 
  INNER JOIN cita_solicituds ON citas.id=cita_solicituds.cita_id 
  INNER JOIN users ON citas.user_id = users.id) q, horario h
where q.horario_id = h.horario.id
于 2013-08-22T17:28:35.977 回答