0

我有一个这样的查询:

$sql_place = "SELECT * FROM place
INNER JOIN join_appointment_place 
ON join_appointment_place.id_place = place.id_place
INNER JOIN join_event_appointment 
ON join_appointment_place.id_appointment = join_event_appointment.id_appointment
WHERE join_event_appointment.id_event = " . $EVENT_ID . "
ORDER BY place.title, place.category";

问题是我需要返回所有不同的地方titles。所以我应该使用DISTINCT. 但是如何选择DISTINCT title并同时返回所有其他值?

  $sql_place = "SELECT 
  DISTINCT 
  place.id_place, 
  place.avatar, 
  place.category, 
  place.title, 
  place.description, 
  place.address, 
  place.latitude,
  place.longitude, 
  place.email, 
  place.web, 
  place.shared FROM place
  INNER JOIN join_appointment_place 
  ON join_appointment_place.id_place = place.id_place
  INNER JOIN join_event_appointment 
  ON join_appointment_place.id_appointment = join_event_appointment.id_appointment
  WHERE join_event_appointment.id_event = " . $EVENT_ID . "
  ORDER BY place.title, place.category";

以上是我试过的。但我担心将 DISTINCT 不仅应用于title,而且应用于每个选定的列,这不是我想要的。谢谢你的帮助 :)

4

4 回答 4

1

如果您只想获得这些标题,请尝试使用 GROUP BY place.title。

于 2013-07-02T08:02:44.883 回答
0

是的,它会在每一列上返回一个不同的值,但这不是坏事。例如,像这样的不同查询可能会返回:

Col1    Col2    Col3    Col4
1       ABC     XYZ     Y
1       ABC     XYZ     N

虽然您的问题听起来像是您期望它只为每一列检索不同的值。

于 2013-07-02T08:02:51.750 回答
0

无论如何,正确的查询是使用GROUP BY Thanks。

$sql_place = "SELECT * FROM place
                  INNER JOIN join_appointment_place ON join_appointment_place.id_place = place.id_place
                  INNER JOIN join_event_appointment ON join_appointment_place.id_appointment = join_event_appointment.id_appointment
                  WHERE join_event_appointment.id_event = " . $EVENT_ID . "
                  GROUP BY place.title ORDER BY place.title, place.category";
于 2013-07-02T08:14:58.590 回答
-1

如果您担心,可以使用嵌套查询。

选择不同的标题并在外部查询的 where 子句中使用结果。

例如

SELECT * FROM Customers
WHERE title IN (SELECT DISTINCT title FROM titles WHERE title > 'X');
于 2013-07-02T08:02:19.980 回答