微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

SQL-JOIN语句创建重复条目

如何解决SQL-JOIN语句创建重复条目

我是sql的新手,我遇到了麻烦。

我有3张桌子:

CREATE TABLE indexCodes 
{
   (indexnum VARCHAR(5) PRIMARY KEY,courseString VARCHAR(10),title VARCHAR(20)
}

CREATE TABLE userid
{
     (id  INT NOT NULL PRIMARY KEY AUTO_INCREMENT,email VARCHAR(255) NOT NULL,password VARCHAR(255) NOT NULL)
}

    CREATE TABLE snipes
{ 
      (snipeNumber  INT NOT NULL PRIMARY KEY AUTO_INCREMENT),FOREIGN KEY indexnum REFERENCES indexcodes(indexnum),FOREIGN KEY userID REFERENCES userid(id)
}

使用JOIN语句

SELECT  userid.id,userID.email,snipes.indexnum,indexcodes.courseString,== indexcodes.title
FROM userid JOIN
     snipes JOIN
     indexcodes ON indexcodes.indexnum = snipes.indexnum 

我遇到重复的条目。 例如,基于一个用户插入的索引代码显示所有用户的索引

解决方法

尝试使用这两个选项

  1. GROUP BY子句

    SELECT userid.id,userID.email,snipes.indexNum,indexcodes.courseString,== indexcodes.title FROM userid JOIN片段 在indexcodes.indexNum = snipes.indexNum上加入索引代码GROUP BY userid.id

  2. DISTINCT

    选择DISTINCT userid.id,userID.email,snipes.indexNum,indexcodes.courseString,== indexcodes.title FROM userid JOIN片段 在indexcodes.indexNum = snipes.indexNum上加入索引代码

,

MySQL是唯一支持JOIN且没有相应的ON子句的数据库。它确实应该返回错误!

您缺少JOIN条条件:

SELECT userid.id,userID.email,snipes.indexNum,indexcodes.courseString,== indexcodes.title
FROM user
     snipes
     ON snipes.userid = user.id JOIN
     indexcodes
     ON indexcodes.indexNum = snipes.indexNum ;

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。