dougou5852 2011-06-04 17:13
浏览 8
已采纳

PHP / MySQL - 订购行

I'm using a row to return all of the users for a particular project, which isn't a problem. However, how would I order it so that the first result is always the logged in user.

I see two solutions, both of which I've tried with no success:

  1. Printing the user's name then using an if statement to only print the rest of the row if they are different from the user's name.

    print user's name;
    if (username !== user's name) {
        print this name;
    }
    
  2. Printing them all in a row but ordering it somehow so that the current user is at the top.

Any ideas?

EDIT

I'm trying it now with the following code:

$query7 = mysql_query("SELECT users_ID FROM projects_users WHERE projects_id = '$id' AND WHERE users_id <>'$q6[0]'") or die(mysql_error());

But I get the error:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE users_id <>'1'' at line 1

I've tried != and NOT instead of <> but I can't see what's going wrong!

Never mind, fixed it. For future reference, don't sure WHERE twice ;)

Thanks for your help everyone.

  • 写回答

4条回答 默认 最新

  • dongzhang7382 2011-06-04 17:17
    关注

    You first solution is perfect.

    If you want to use your second solution you should build your SQL query like this:

    (SELECT * FROM user WHERE id = :currentUserID) UNION (SELECT * FROM user)
    

    And you are good

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 r语言神经网络自变量重要性分析
  • ¥15 基于双目测规则物体尺寸
  • ¥15 wegame打不开英雄联盟
  • ¥15 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)
  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢