我已经查看了各种SO问题,了解如何进行计数和排名。我的数据库中有一个名为vDestination的字段,我想得到每个目的地的计数和排名。我正在使用Codeigniter。我有以下内容:
$sql = ("SET @curRank := 0;\r\n
SELECT vDestination, COUNT(*) AS count, @curRank := @curRank + 1 AS rank FROM tbl_user_trip ORDER BY vDestination");
这看起来像有效的SQL语法,但我不断收到数据库错误。有人请指出这个查询有什么问题吗?
发生数据库错误
错误号码:1064您的SQL语法有错误;检查与MySQL服务器版本对应的手册,以便在第3行使用“SELECT vDestination,COUNT(*)AS count,@curRank:= @curRank + 1 AS rank FROM tb”附近使用正确的语法
SET @curRank := 0; SELECT vDestination, COUNT(*) AS count, @curRank := @curRank + 1 AS rank FROM tbl_user_trip ORDER BY vDestination
这是我得到的错误。
答案 0 :(得分:1)
工作解决方案如下:
public partial class MainWindow : Window, INotifyPropertyChanged
{
string _name;
public event PropertyChangedEventHandler PropertyChanged;
public string PersonName
{
get => _name;
set
{
_name = value;
OnPropertyChanged("PersonName");
}
}
public MainWindow()
{
InitializeComponent();
_name = "Adam";
PersonName = "Roman";
}
protected void OnPropertyChanged(string name)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(name));
}
}
}
正如@tyro建议的那样,但也有GROUP BY。