简体   繁体   中英

How to fix query in mysql

I'm creating a view table in mysql i am facing a problem in mysql query

CREATE VIEW `vwStockin` AS 
SELECT id
     , refno
     , pcode
     , qty 
     , sdate
     , stockinby 
  FROM tbl_product
  JOIN tbl_stockin 
    ON tbl_product.pcode = tbl_stockin.pcode

It shows the following error: 1052 - Column 'pcode' in field list is ambiguous

You must add the table name to the columns with same name present in both the joined table

    CREATE VIEW `vwStockin` 
    AS SELECT id
             ,refno
             ,tbl_product.pcode
             ,qty
             ,sdate
             ,stockinby 
    FROM `tbl_product` 
    INNER JOIN tbl_stockin ON `tbl_product`.pcode = `tbl_stockin`.pcode

generally is better use a fully qualified name for joined tables and use alias for a better reading eg:

    CREATE VIEW `vwStockin` 
    AS SELECT p.id
             ,p.refno
             ,p.pcode
             ,s.qty
             ,s.sdate
             ,s.stockinby 
    FROM `tbl_product` p 
    INNER JOIN tbl_stockin  s ON p.pcode =  s.pcode

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