简体   繁体   English

如何检查登录的用户ID并在MySQL查询中首先返回该行?

[英]How do I check for logged in user id and return that row first in a MySQL query?

The query below selects the 'loves' on an item. 下面的查询选择项目上的“loves”。 (think of it as similar to facebooks 'like' system. (想想它类似于facebook'''系统。

There are two tables in use in this select. 此选择中使用了两个表。 A link table (containing itemid, userid, lovetime) and this is joined to a users table in order to retrieve the username/user profile url etc. 一个链接表(包含itemid,userid,lovetime),并将其连接到users表,以便检索用户名/用户配置文件URL等。

$lovequery = "select love.lovetime, love.userid as ID, love_users.display_name, love_users.user_url
                    from ".$wpdb->prefix."comment_loves love
                    left join ".$wpdb->prefix."users love_users on love_users.ID=love.userid
                    where commentid = $itemid                   
                    order by love.lovetime desc
                    limit 4

             "; 

The results are limited to 4 because I simply do not need any more data. 结果限制为4,因为我根本不需要任何更多数据。 The total count is stored separately in the actual item table to reduce queries. 总计数分别存储在实际项目表中以减少查询。

Once the rows are retrieved from this query I iterate through the array, cross referencing against the total 'lovecount' and build a text string formatted like so: 从这个查询中检索行后,我遍历数组,交叉引用总'lovecount'并构建一个如下格式的文本字符串:

You, John Smith, Joe Bloggs and 4 others love this.

This works fine however it fails if the logged in user (YOU) does not have the most recent 'lovetime' 这工作正常,但如果登录用户(您)没有最近的“恋爱时间”它会失败

What I want to do is have the currently logged in use always at the top of the returned results even if his/her 'lovetime' is older than the 4 most recent ones so that the string always begins with 'You' if the logged in user has 'loved' this item. 我想要做的是将当前登录的使用始终放在返回结果的顶部,即使他/她的'lovetime'比最近的4个还要早,所以如果登录,则字符串始终以'You'开头用户已经“喜欢”了这个项目。

The logged in user id is available in the script as $userid. 登录的用户标识在脚本中以$ userid的形式提供。

To clarify 澄清

if I have the following table (the timestamps are written as simple UK dates for legibility purposes):- 如果我有下表(为便于阅读,时间戳写为简单的英国日期): -

    userid  commentid  lovetime

    34       3          02/10/2011
    24       3          03/10/2011
    13       3          06/10/2011
    65       3          14/10/2011
    1*       3          10/09/2011

* with userid 1 being the logged in user id

I would only get user id's 34,24,13,65 returned in that order due to ordering by 'lovetime' 由于'lovetime'的订购,我只会按顺序返回用户ID 34,24,13,65

What I want is for the results to return ideally 1,34,24,65. 我想要的是结果理想地返回1,34,24,65。 if that proves too tricky then getting 5 total rows when the userid exists would be okay also. 如果这证明太棘手,那么当用户ID存在时总共获得5行也是可以的。

I hope this is clear enough, it was rather difficult to articulate. 我希望这很清楚,很难说清楚。

How would I go about modifying the query to ensure the results are as described. 我将如何修改查询以确保结果如上所述。

Many thanks. 非常感谢。

您可以通过ORDER BY (ID = "auth_user_id") DESC等条件来订购结果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM