C#中MySQL查询出错?

时间:2018-01-24 01:37:23

标签: c# mysql .net

我在MySQL查询中收到意外错误,有人可以帮我发现原因吗?

错误讯息:

  

MySql.Data.MySqlClient.MySqlException(0x80004005):您有错误   在你的SQL语法中;查看与MariaDB对应的手册   服务器版本,用于在&users;。users =。附近使用正确的语法   user_auth_tickets.user_id WHERE user_auth_tickets.auth_ticket =' N'在   第1行

查询:

dbConnection.SetQuery("SELECT users.id,users.username,users.rank,users.motto,users.look,users.gender,users.last_online,users.credits,users.activity_points,users.home_room,users.block_newfriends,users.hide_online,users.hide_inroom,users.vip,users.account_created,users.vip_points,users.machine_id,users.volume,users.chat_preference,users.focus_preference,users.pets_muted,users.bots_muted,users.advertising_report_blocked,users.last_change,users.gotw_points,users.ignore_invites,users.time_muted,users.allow_gifts,users.friend_bar_state,users.disable_forced_effects,users.allow_mimic,users.rank_vip " +
  "FROM users" +
  "JOIN user_auth_tickets " +
  "ON users.id = user_auth_tickets.user_id " +
  "WHERE user_auth_tickets.auth_ticket = @sso AND last_ip = @lastIp " +
  "LIMIT 1"
);

1 个答案:

答案 0 :(得分:2)

您的查询中有两个WHERE子句。 看起来你可能有一个较短的初始查询,你确实正确地放置了WHERE子句,但后来决定JOIN其他表并假设其他条件。那时您需要重新构建查询,根据下面引用中的语法层次结构,WHERE来自表引用后的

OPs Question

Per the MySQL 5.6 Reference Manual - SELECT Syntax

  

WHERE子句(如果给出)表示条件或条件   行必须满足才能被选中。 where_condition是一个表达式   对于要选择的每一行,计算结果为true。该声明   如果没有WHERE子句,则选择所有行。

删除第一个WHERE,然后将last_ip = @lastIp追加到较低的WHERE

例如:

dbConnection.SetQuery("SELECT users.id,users.username,users.rank,users.motto,users.look,users.gender,users.last_online,users.credits,users.activity_points,users.home_room,users.block_newfriends,users.hide_online,users.hide_inroom,users.vip,users.account_created,users.vip_points,users.machine_id,users.volume,users.chat_preference,users.focus_preference,users.pets_muted,users.bots_muted,users.advertising_report_blocked,users.last_change,users.gotw_points,users.ignore_invites,users.time_muted,users.allow_gifts,users.friend_bar_state,users.disable_forced_effects,users.allow_mimic,users.rank_vip " +
"FROM users " +
"JOIN user_auth_ticket ON users.id = user_auth_ticket.user_id " +
"WHERE user_auth_ticket.auth_ticket = @sso AND last_ip = @lastIp " +
"LIMIT 1"
);