Mysql Query只选择第一行而不是全部。

时间:2015-08-29 02:35:36

标签: php mysql

您好我想问一下,有人可以帮我解决这个问题,而这个问题是由Barmer帮我构建的。但是它发生了什么,查询只选择一行数据,即表中的第一行。

<body>

  <div id="card">
    <p class="name">
      Sam Fellig</p>
    <p class="add">New York,US</p>
    <p class="info">From a non-technical guy with an idea to building one of TIME's Top 50 sites of 2013, Sam Fellig's story is nothing less than magical. But the founder of Outgrow.me says anyone can learn, as long as they stay positive.</p>
  </div>
</body>

我需要的是从cursos_modluos中选择所有行,现在只选择一行。如果有人能告诉我可能解决的问题,我会非常感激!

2 个答案:

答案 0 :(得分:1)

您正在使用没有group by的聚合函数。这样的查询总是只返回一行。

目前尚不清楚你想做什么,但我可能会猜到添加:

GROUP BY c.id

会做你想做的事。

答案 1 :(得分:1)

应该是

SELECT c.*, count(s.curso_id) as count, SUM(IF(s.status = "aprobado", 1, 0)) AS count_approved , SUM(IF(s.status = "cupolleno", 1, 0)) AS count_cupolleno
, SUM(IF(s.status = "cancelado", 1, 0)) AS count_cancelado, SUM(IF(s.status = "noacion", 1, 0)) AS count_noacion, SUM(IF(s.status = "ama_de_casa", 1, 0)) AS count_ama_de_casa
, SUM(IF(s.status = "cliente_externo", 1, 0)) AS count_cliente_externo
FROM cursos_modulos AS c
LEFT JOIN subscriptions AS s ON s.curso_id = c.id
LEFT JOIN users AS u ON u.userID = s.user_id
GROUP BY c.id <<----