首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MySQL order By issue

MySQL order By issue
EN

Stack Overflow用户
提问于 2011-04-26 22:51:49
回答 2查看 110关注 0票数 0

嗨,简单的问题,我猜,但不知道如何列出mysql的方式,我想它。

基本上,在一行中我有CityID,我希望能够取出CityID的== 14,并在返回的顶部显示它们(但不是作为A计数)

例如,珀斯== 15墨尔本== 14普雷斯顿== 14悉尼== 13

目前他们这样展示悉尼== 13珀斯== 15墨尔本== 14普雷斯顿== 14

我的代码

$sth = mysql_query("SELECT users.id as id, users.username as username, profile.defaultpictureid as picture FROM users, userprofiles as profile WHERE online = '1' AND profile.country = ".$this->country." AND profile.state = ".$this->state." AND profile.city = ".$this->city." ORDER BY if (profile.city = 12276,0,1)");

上面的代码现在似乎可以工作了。

然而,似乎还打印了两次数据。

{"id":"7",“用户名”:“A”,“图片”:“0”},{"id":"1",“用户名”:“B”,“图片”:“0”},{"id":"1",“用户名”:“B”,“图片”:“1”},{"id":"7",“用户名”:“A”,“图片”:“1”}

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-04-26 23:40:41

您正在从两个表(用户和配置文件)中进行选择,但是在where子句中没有指定when之间的任何类型的关系,所以您得到的是这两个表的笛卡尔乘积,这就是为什么您会得到重复的结果。

我猜你的查询应该看起来像这样:

代码语言:javascript
复制
SELECT users.id as id, users.username as username, profile.defaultpictureid as picture
FROM users, userprofiles as profile
WHERE
    online = 1 AND
    profile.country = {$this->country} AND
    profile.state = {$this->state} AND
    profile.city = {$this->city} AND 
    users.id = userprofiles.userid     <---the join condition for the two tables
ORDER BY if (CityID = 14, 1, 0), profile.city
票数 2
EN

Stack Overflow用户

发布于 2011-04-26 22:54:15

您可以在排序中应用if子句

代码语言:javascript
复制
order by if(CityID = 14,0,1)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5792176

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档