简体   繁体   中英

Mysql create view error 1052

Hello stackoverflow people

I have a 2 tables that shares the same attributes, but in different categories. I am trying to list items ONLY with the attribute "Rending":

CREATE VIEW rending AS SELECT ranged_weapons.Name_, ranged_weapons.Dam, ranged_weapons.Dam_Type,
melee_weapons.Name_, melee_weapons.Dam, melee_weapons.Dam_Type
FROM ranged_weapons, melee_weapons 
WHERE Dam_Type = 'Rending';

but when i run it, i get:

Error Code: 1052. Column 'Dam_Type' in where clause is ambiguous

What am i doing wrong?

table information

  CREATE TABLE Ranged_weapons (
  Name_             varchar (40) NOT NULL,
  Class             varchar (40),
  Type_             varchar (40),
  Range_            varchar (40),
  RoF               varchar (40),
  Dam               varchar (40),
  Dam_Type          varchar (10),
  Pen               integer     ,
  Clip              integer     ,
  Rld               varchar (10),
  PRIMARY KEY (Name_),
  FOREIGN KEY (Name_) REFERENCES Items(Name_) ON DELETE CASCADE );


  CREATE TABLE Melee_weapons (
  Name_             varchar (40) NOT NULL,
  Type_             varchar (40),
  Dam               varchar (40),
  Dam_Type          varchar (40),
  Pen               integer     ,
  PRIMARY KEY (Name_),
  FOREIGN KEY (Name_) REFERENCES Items(Name_) ON DELETE CASCADE );

Thats says it all meaning the column is present in both the tables and you need to explicitly mention which table column you are referring to something as

CREATE VIEW rending AS SELECT *
FROM ranged_weapons, melee_weapons 
WHERE ranged_weapons.Dam_Type = 'Rending';

UPDATE : From the given table structures using the above code will show duplicate column name error since lot of column name is same across the tables.

The best thing is to select the columns explicitly for the view using JOIN or if needed to select the same column from the other table then using different alias names. Here is an example of how we can select and create the view using JOIN

CREATE VIEW rending AS 
SELECT rw.Name_,rw.Class,rw.Type_,rw.Range_,
rw.RoF,rw.Dam,rw.Dam_Type,rw.Pen ,rw.Clip,rw.Rld
FROM ranged_weapons rw
JOIN melee_weapons mw on mw.Name_ = rw.Name_
WHERE rw.Dam_Type = 'Rending';

If you want to select same columns for the view from different tables this how you can use alias

CREATE VIEW rending AS 
SELECT rw.Name_,rw.Class,rw.Type_,rw.Range_,
rw.RoF,rw.Dam,rw.Dam_Type,rw.Pen ,rw.Clip,rw.Rld,
mw.Name_ as mw_Name
FROM ranged_weapons rw
JOIN melee_weapons mw on mw.Name_ = rw.Name_
WHERE rw.Dam_Type = 'Rending';

Here mw.Name_ as mw_Name will refer to the column from melee_weapons and you can specify other column names this way if its needed for the view.

DAM_TYPE is present in both tables that's why where clause unable to recognize it...

You must write table_name with DAM_TYPE in where clause..

Thanks

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM