【问题标题】:mysql delete with cross table lookup [closed]mysql删除与交叉表查找[关闭]
【发布时间】:2014-06-14 01:34:29
【问题描述】:

SQL

DELETE FROM sex s, users u WHERE s.id = 195 and u.id = s.uid and u.sessionCheck = 'd986a074c7549c566bfed1d4ad7ca491'

错误

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 's, users u WHERE s.id = 195 and u.id = s.uid and u.sessionCheck = 'd986a074c7549' at line 1

很明显错误在于删除中的连接。但是我尝试过的一切都行不通。

我正在使用Server version: 5.6.16 - MySQL Community Server (GPL)

【问题讨论】:

  • 这个问题是由一个简单的印刷错误引起的。虽然类似的问题可能是这里的主题,但这个问题的解决方式不太可能帮助未来的读者。
  • @JamieHutber 你一定是用错了谷歌。我搜索并返回了 102,000 个结果。
  • @Kermit 那是因为谷歌没有文档......
  • @JamieHutber,您应该以更有意义的方式重组该句子。

标签: mysql sql


【解决方案1】:
DELETE s /* forgot to mention from which table you like to delete from these 2 */
FROM sex s
join users u on u.id = s.uid 
WHERE s.id = 195
and u.sessionCheck = 'd986a074c7549c566bfed1d4ad7ca491'

【讨论】:

  • 所以等等...这是什么DELETE S 这是删除整行,S 是对sex 的引用?
  • 是的,加入时必须指定从哪些表中删除。是delete table_to_delete_from from table_to_delete_from join ...
  • bingo :) 非常感谢,很有意义
  • 顺便说一句,您可以指定多个表并一次从多个表中删除。
【解决方案2】:
DELETE s.* FROM sex s,users u   WHERE u.id = s.uid AND s.id = 195 AND u.sessionCheck = 'd986a074c7549c566bfed1d4ad7ca491'

【讨论】:

    【解决方案3】:
    DELETE FROM sex 
    WHERE sex.id IN (SELECT id 
                     FROM users u
                     WHERE u.sessionCheck = 'd986a074c7549c566bfed1d4ad7ca491')
    

    从两个表中删除

    DELETE s.*,u.*
    FROM sex s
    inner join users u on u.id = s.uid 
    WHERE s.id = 195
    and u.sessionCheck = 'd986a074c7549c566bfed1d4ad7ca491'
    

    【讨论】:

      猜你喜欢
      • 2017-03-06
      • 1970-01-01
      • 2012-07-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-15
      • 1970-01-01
      相关资源
      最近更新 更多