在postgresql中组合三行/表

时间:2016-10-10 19:48:30

标签: sql postgresql

我必须连接三个表。我的代码适用于两个表,但不适用于三个表。我在Debian中使用PostgreSQL 9.4.9。

包含2个表的代码(WORKS):

SELECT 
manufactures.manufacturename
,products.productname
FROM products
INNER JOIN manufactures
ON manufactures.manufactureid=products.productmanufacturer;

包含3个表的代码(不会有效)

SELECT 
manufactures.manufacturename
,products.productname
,languages.languagename
FROM products
INNER JOIN manufactures
ON manufactures.manufactureid=products.productmanufacturer
INNER JOIN languages
ON languages.languagename=products.productlanguage;

错误消息

baza_testowa=# SELECT 
baza_testowa-# manufactures.manufacturename
baza_testowa-# ,products.productname
baza_testowa-# ,languages.languagename
baza_testowa-# FROM products
baza_testowa-# INNER JOIN manufactures
baza_testowa-# ON manufactures.manufactureid=products.productmanufacturer
baza_testowa-# INNER JOIN languages
baza_testowa-# ON languages.languagename=products.productlanguage;
ERROR:  operator does not exist: text = integer
LINE 9: ON languages.languagename=products.productlanguage;
                                 ^
HINT:  No operator matches the given name and argument type(s). You might need to add explicit type casts.


     Table "public.products"
       Column        |  Type   |                          Modifiers                           
---------------------+---------+--------------------------------------------------------------
 productid           | integer | not null default nextval('products_productid_seq'::regclass)
 productmanufacturer | integer | not null
 productname         | text    | not null
 productlanguage     | integer | 
Indexes:
    "products_pkey" PRIMARY KEY, btree (productid)


                                 Table "public.manufactures"
     Column      |  Type   |                              Modifiers                               
-----------------+---------+----------------------------------------------------------------------
 manufactureid   | integer | not null default nextval('manufactures_manufactureid_seq'::regclass)
 manufacturename | text    | not null
Indexes:
"manufactures_pkey" PRIMARY KEY, btree (manufactureid)


      Table "public.languages"
    Column    |  Type   |                           Modifiers                            
--------------+---------+----------------------------------------------------------------
 languageid   | integer | not null default nextval('languages_languageid_seq'::regclass)
 languagename | text    | not null
Indexes:
    "languages_pkey" PRIMARY KEY, btree (languageid)

2 个答案:

答案 0 :(得分:1)

languages.languagename是text但product.productlanguage是整数,你不能比较它们

答案 1 :(得分:0)

您无法在不同类型的列上进行 JOIN 。如果您确实希望products.productlanguage为整数且languages.languagename为文本,则可以使用显式类型转换。所以在你的情况下它应该是这样的:

SELECT manufactures.manufacturename, products.productname, languages.languagename
FROM products
INNER JOIN manufactures ON manufactures.manufactureid = products.productmanufacturer
INNER JOIN languages ON languages.languagename = products.productlanguage::text;