0

如何在不写长全名的情况下选择 p.Quota?现在我在做

SELECT c.id, 
       c.UserName, 
       p.Quota, 
       cs.StatusName 
  FROM CUSTOMERS AS c, 
       PRODUCTS AS p 
 LEFT JOIN CUSTOMERSTATUSTYPES as cs ON c.StatusId=cs.CustomerStatusId 
 LIMIT 1 ;

我得到错误:

ERROR 1054 (42S22): Unknown column 'c.StatusId' in 'on clause'

但是该列确实退出并且此代码有效:

SELECT c.id, 
       c.UserName, 
       cs.StatusName 
  FROM CUSTOMERS AS c
  JOIN CUSTOMERSTATUSTYPES as cs ON c.StatusId = cs.CustomerStatusId 
 LIMIT 1 ;
4

3 回答 3

2

您将 ANSI 和非 ANSI JOIN 语法与:

SELECT c.id, 
       c.UserName, 
       p.Quota, 
       cs.StatusName 
  FROM CUSTOMERS AS c, 
       PRODUCTS AS p 
  LEFT JOIN CUSTOMERSTATUSTYPES as cs ON c.StatusId=cs.CustomerStatusId 
  LIMIT 1 ;

使用 ANSI 连接编写:

     SELECT c.id, 
            c.UserName, 
            p.Quota, 
            cs.StatusName 
       FROM CUSTOMERS AS c 
       JOIN PRODUCTS AS p ON --JOIN criteria goes here
  LEFT JOIN CUSTOMERSTATUSTYPES as cs ON c.StatusId = cs.CustomerStatusId 
      LIMIT 1;

...但我不知道您使用什么标准加入PRODUCTS表格CUSTOMERS

于 2009-09-25T15:50:41.723 回答
2

问题是您的隐式内部联接后跟左联接。MySQL 正在尝试在 CUSTOMERSTATUSTYPES c 上加入 PRODUCTS p,而不考虑 CUSTOMERS c。

试试这个:

SELECT 
 c.id, c.UserName, p.Quota, cs.StatusName 
FROM 
 CUSTOMERS AS c 
 INNER JOIN PRODUCTS AS p 
 LEFT JOIN CUSTOMERSTATUSTYPES as cs ON c.StatusId=cs.CustomerStatusId 
LIMIT 1 

此外,现在您没有将 CUSTOMERS 中的记录与 PRODUCTS 中的记录相关联的条款……您只是在进行完全联接。这是你想做的吗?

于 2009-09-25T15:52:48.160 回答
0
SELECT c.id, 
       c.UserName, 
       p.Quota, 
       cs.StatusName 
FROM (CUSTOMERS AS c, PRODUCTS AS p) 
LEFT JOIN CUSTOMERSTATUSTYPES as cs ON c.StatusId=cs.CustomerStatusId 
LIMIT 1 ;
于 2009-09-25T15:49:56.183 回答