English 中文(简体)
在不工作的地方
原标题:MYSQL HAVING WHERE not working

我有以下功能

function availableChatRoom($topic_id){
$max_rooms = 2;
$max_users = 2;

$sqli = "SELECT COUNT(DISTINCT room_num) AS numRooms FROM forum_chat WHERE topic_id =  $topic_id ";
$num_rooms = queryColumnVal($sqli, "numRooms");

$sqlii = "SELECT room_num, COUNT(user_id) AS numUsers FROM forum_chat 
          WHERE topic_id =  $topic_id  AND (status =  online  AND status =  offline ) AND chat_msg =   
          GROUP BY room_num
          HAVING numUsers < $max_users ORDER BY msg_date";

$num_rooms_with_spaces = mysqlNumRows($sqlii, "room_num");
queryColumnVal($sqlii, "room_num");

if($num_rooms == 0){
    return 1;
时 时
elseif($num_rooms <= $max_rooms){

    if($num_rooms_with_spaces == 0){
        return $num_rooms + 1;
    时 时
    else{
        return queryColumnVal($sqlii, "room_num");
    时 时
时 时
else{
    return "none";
时 时   

时 时

the idea is for the function to return a chat room number with space. -Every time a user joins the room, a line with no msg is inserted with status online -Every time a user leaves the room a line with no msg is inserted with status offline I am now trying to write a function which checks if there are any spare rooms for each topic The idea is to select the room_num where the number of users who have logged in but not logged out is less than the maximum chatter per room. Can someone help me with my $sqlii.

谢谢 谢谢

最佳回答

您的查询中有几个错误 :

  • (status = online AND status = offline )

这表示您的查询 wil 返回时完全没有 。 请使用: < code> (status = 在线 OR status = 离线) 或等效的 < code> (status IN (线上, 离线)) 或等效的 < code > (status IN (线上, 离线))

  • COUNT(user_id)

这不计算线上离线用户与线上离线用户的差别。 请更正逻辑 。

  • ORDER BY msg_date

GROUP by 查询 查询中,尽管 MySQL 允许使用 haining SELECT ORDER 条款,但使用非聚合列是不妥的。除非该栏取决于分组表达式。由于 msg_date 显然不依赖于 room_num ,所以您应该用一个综合功能替换:

ORDER by MAX(msg_date) ORDER by MIN(msg_date) (msg_date)


$sqlii = "
   SELECT room_num, 
          COUNT(CASE WHEN status =  online  THEN 1 END) 
            - COUNT(CASE WHEN status =  offline  THEN 1 END) 
          AS numUsers 
   FROM forum_chat 
   WHERE topic_id =  $topic_id  
     AND status IN ( online ,  offline )
     AND chat_msg =   
   GROUP BY room_num
   HAVING COUNT(CASE WHEN status =  online  THEN 1 END) 
          - COUNT(CASE WHEN status =  offline  THEN 1 END) 
          < $max_users 
   ORDER BY MAX(msg_date)
     ";
问题回答

HAVING 条款 是在 SELECT 之前进行“强”评估的,因此服务器还不知道该别名 。

SELECT room_num, COUNT(user_id) AS numUsers 
FROM forum_chat 
WHERE topic_id =  $topic_id  AND 
      (status =  online  OR status =  offline ) AND 
      chat_msg =   
GROUP BY room_num
HAVING COUNT(user_id) < $max_users ORDER BY msg_date
  1. First the product of all tables in the from clause is formed.
  2. The where clause is then evaluated to eliminate rows that do not satisfy the search_condition.
  3. Next, the rows are grouped using the columns in the group by clause.
  4. Then, Groups that do not satisfy the search_condition in the having clause are eliminated.
  5. Next, the expressions in the select clause target list are evaluated.
  6. If the distinct keyword in present in the select clause, duplicate rows are now eliminated.
  7. The union is taken after each sub-select is evaluated. 8.Finally, the resulting rows are sorted according to the columns specified in the order by clause.

You cannot use the alias of the column in HAVING in the same SELECT level. That is because in yout MySQL ONLY_FULL_GROUP_BY would be activated.

试试这个

HAVING COUNT(user_id) < $max_users ORDER BY msg_date




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签