我的sql语法出现问题我收到错误:
ERROR [HY000] [MySQL][ODBC 3.51 Driver][mysqld-5.5.9]Not unique table/alias: 'WallPosting'
代码:
{
string theUserId = Session["UserID"].ToString();
using (OdbcConnection cn = new OdbcConnection("Driver={MySQL ODBC 3.51 Driver}; Server=localhost; Database=gymwebsite2; User=x; Password=x;"))
{
cn.Open();
using (OdbcCommand cmd = new OdbcCommand("SELECT WallPosting.Wallpostings FROM WallPosting LEFT JOIN WallPosting ON User.UserID = WallPosting.UserID WHERE User.UserID=" + theUserId + "", cn))
// problem in select statement
using (OdbcDataReader reader = cmd.ExecuteReader())
{
var divHtml = new System.Text.StringBuilder("<div id=mysqlcontent>");
while (reader.Read())
{
divHtml.Append(String.Format("{0}", reader.GetString(0)));
}
divHtml.Append("</div>");
test1.InnerHtml = divHtml.ToString();
}
}
}
我的sql脚本和表结构:
SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='TRADITIONAL';
CREATE SCHEMA IF NOT EXISTS `gymwebsite2` DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci ;
USE `gymwebsite2` ;
-- -----------------------------------------------------
-- Table `gymwebsite2`.`User`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `gymwebsite2`.`User` (
`UserID` INT NOT NULL AUTO_INCREMENT ,
`Email` VARCHAR(245) NULL ,
`FirstName` VARCHAR(45) NULL ,
`SecondName` VARCHAR(45) NULL ,
`DOB` VARCHAR(45) NULL ,
`Location` VARCHAR(45) NULL ,
`Aboutme` VARCHAR(1045) NULL ,
`username` VARCHAR(45) NULL ,
`password` VARCHAR(45) NULL ,
PRIMARY KEY (`UserID`) )
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `gymwebsite2`.`WallPosting`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `gymwebsite2`.`WallPosting` (
`idWallPosting` INT NOT NULL AUTO_INCREMENT ,
`UserID` INT NOT NULL ,
`Wallpostings` VARCHAR(2045) NULL ,
PRIMARY KEY (`idWallPosting`) ,
INDEX `fk_WallPosting_User` (`UserID` ASC) ,
CONSTRAINT `fk_WallPosting_User`
FOREIGN KEY (`UserID` )
REFERENCES `gymwebsite2`.`User` (`UserID` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `gymwebsite2`.`Pictures`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `gymwebsite2`.`Pictures` (
`idPictures` INT NOT NULL AUTO_INCREMENT ,
`UserID` INT NOT NULL ,
`picturepath` VARCHAR(1045) NULL ,
PRIMARY KEY (`idPictures`) ,
INDEX `fk_Pictures_User1` (`UserID` ASC) ,
CONSTRAINT `fk_Pictures_User1`
FOREIGN KEY (`UserID` )
REFERENCES `gymwebsite2`.`User` (`UserID` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
SET SQL_MODE=@OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
答案 0 :(得分:2)
当您执行自联接(将表连接到自身)时,您需要为至少一个表实例提供别名,以便SQL知道您在任何给定时间谈论的实例。像... LEFT JOIN WallPosting AS a ON ...
这样的东西,然后在表示连接表时使用a.WallPosting
。
答案 1 :(得分:1)
不应该是
"SELECT WallPosting.Wallpostings FROM WallPosting LEFT JOIN User ON User.UserID = WallPosting.UserID WHERE User.UserID=" + theUserId + ""
您再次加入WallPosting
表(这需要您指定别名),但是如果要使用{{1}中的列,则应该加入User
表。表格。
答案 2 :(得分:1)
更正您的查询
SELECT WallPosting.Wallpostings FROM WallPosting LEFT JOIN USER ON User.UserID = WallPosting.UserID WHERE User.UserID=" + theUserId + "", cn
你做的是
WallPosting LEFT JOIN WALLPOSTING ON User.UserID = WallPosting.UserID