[英]Mysql Select All from one table, and some from another table Using Aliases
I'm posting this question because I did manage to find a similar question , but they weren't using aliases. 我发布这个问题是因为我确实设法找到了一个类似的问题 ,但是他们没有使用别名。
I have two tables - I want to grab everything from table1
and just grab the user_name
and Team
from table2
. 我有两个表-我想从table1
获取所有内容,而仅从table2
获取user_name
和Team
。
My original query is grabbing everything from table2
我原来的查询是从table2
获取所有内容
SELECT *
FROM qabin.allqas t1
JOIN login.users t2
ON (t1.Submitter = t2.user_name)
WHERE t1.Status='Complete'
That's all well and good and works fine, but I would like to just get the user_name
and the Team
一切都很好,并且工作正常,但是我只想获取user_name
和Team
To make things more interesting, they are in different databases, though it hasn't been an issue. 为了使事情变得更加有趣,它们不在不同的数据库中,尽管这不是问题。
One is in the qabin
database and the other is in the login
database. 一个在qabin
数据库中,另一个在login
数据库中。
I have tried: 我努力了:
SELECT
qabin.allqas.* AS t1,
login.users.Team,
login.users.user_name
JOIN login.users t2
ON (t1.Submitter = t2.user_name)
WHERE t1.Status='Complete'
I need the t1 and t2 aliases because they are used elsewhere for building a larger query string. 我需要t1和t2别名,因为它们在其他地方用于构建更大的查询字符串。
Thanks in advance! 提前致谢!
Why not use a subquery? 为什么不使用子查询?
SELECT *
FROM qabin.allqas t1
JOIN (SELECT user_name, Team FROM login.users) t2
ON (t1.Submitter = t2.user_name)
WHERE t1.Status='Complete'
You cant use one alias (t1) for a set of columns. 您不能对一组列使用一个别名(t1)。 I guess what you are looking for is something like: 我想您正在寻找的是这样的:
SELECT t1.*, t2.team, t2.user_name
FROM qabin.allqas t1
JOIN login.users t2
ON (t1.Submitter = t2.user_name)
WHERE t1.Status='Complete'
I would recommend using the real column names from t1 instead of t1.* 我建议使用t1而不是t1的真实列名。*
Try This query : 试试这个查询:
SELECT t1.*, t2.user_name, t2.Team
FROM qabin.allqas t1
JOIN login.users t2
ON t1.Submitter 1= t2.user_name
WHERE t1.Status='Complete'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.