使用交叉联接时如何防止重复的表名?

时间:2019-02-03 00:04:11

标签: sql sqlite

我在SQL数据库中有以下表格:

t_customers
   ID_operator int primary key auto_incr
   firstname   varchar(30)
   lastname    varchar(30)
   email       varchar(100)


t_operator
   ID_operator int primary key auto_incr
   firstname   varchar(30)
   lastname    varchar(30)
   course      varchar(10)

我还有另一个表,它代表一个顺序,在查询另一个字段时,我将两个字段都加入其中:

SELECT 
    *, // table t_orders
    t_customers.firstname, 
    t_customers.lastname, 
    t_operator.firstname AS operator_firstname, 
    t_operator.lastname AS operator_lastname 
FROM 
    t_orders 
CROSS JOIN 
    t_customers, t_operator 
WHERE 
    id_orders IS 1;

我尝试通过使用AS关键字来缓解这种情况,并且确实添加了新字段,但是我在查询中仍然有2个名为“ firstname”和“ lastname”的字段,这使我无法使用它正确地。除了重命名表之外,还有其他解决方案吗?

1 个答案:

答案 0 :(得分:0)

您是否正在尝试做这样的事情?

SELECT o.*, // table t_orders
       c.firstname, c.lastname, 
       op.firstname AS operator_firstname, 
       op.lastname AS operator_lastname 
FROM t_orders o JOIN
     t_customers c
     ON o.id_customer = c.id_customer JOIN
     t_operator op
     ON c.id_operator = c.id_operator
WHERE id_orders = 1;