嗨,简单的问题,我猜,但不知道如何列出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”}
发布于 2011-04-26 23:40:41
您正在从两个表(用户和配置文件)中进行选择,但是在where子句中没有指定when之间的任何类型的关系,所以您得到的是这两个表的笛卡尔乘积,这就是为什么您会得到重复的结果。
我猜你的查询应该看起来像这样:
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发布于 2011-04-26 22:54:15
您可以在排序中应用if子句
order by if(CityID = 14,0,1)https://stackoverflow.com/questions/5792176
复制相似问题