Mysql 从子查询中选择具有相同名称的列

Mysql 从子查询中选择具有相同名称的列,mysql,Mysql,两个表具有相同的名称Id,现在我想使用子查询上的where子句从子查询中获取两个Id列 下面是具有两列p.id和s.id的查询 select name,price,total,user_id,id,id from( SELECT p.name,p.price,s.user_id,s.id,p.id FROM products p,shoping_cart s where p.id=s.product_id ) z where z.user_id=11 首先,您可以尝试使用别名来表示这两列 没

两个表具有相同的名称Id,现在我想使用子查询上的where子句从子查询中获取两个Id列

下面是具有两列p.id和s.id的查询

select name,price,total,user_id,id,id from(
SELECT p.name,p.price,s.user_id,s.id,p.id FROM products p,shoping_cart 
s where p.id=s.product_id
) z where z.user_id=11

首先,您可以尝试使用别名来表示这两列

没有理由在查询中使用子查询,您可以尝试直接选择它

我会使用
join
语法而不是
逗号来连接两个表,因为
表示
交叉连接
这是一种旧样式

SELECT 
    p.name,
    p.price,
    s.user_id,
    s.id 'sid',
    p.id 'pid'
FROM products p JOIN shoping_cart s  on p.id=s.product_id
WHERE s.user_id = 11

使用别名,例如“s.id as sid”,然后基本上重命名了列,您可以在外部查询中使用别名。您好,在子查询中为列指定别名,如下所示:
select…,id\u s,id\u p from(select…,s.id as id\u s,p.id as id\u from…)