简体   繁体   English

Mysql从一个表中全选,从另一个表中全选

[英]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_nameTeam

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_nameTeam

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.

相关问题 从一个表中选择所有值,从另一个表中选择一些值,并用php显示它们 - select all the values from one table and some values from another table and display them with php 如何在cakephp 3.6中从一个表中选择所有记录,并从另一个表中选择一些记录 - how to select all records from one table and some from another table in cakephp 3.6 在mysql中从一个表中选择所有字段,从另一个表中选择一个 - select all field from one table and one from another table in mysql Select 全部来自一个表,但如果它存在于另一个表中则删除 - Select all from one table but remove if it exists from another table 使用PHP和MySQL如何使用另一个表中的某些字段值填充一个表 - Using PHP and MySQL how do I populate one table using some field values from another table MySQL我可以从一个表还是另一个表中选择数据? - MySQL Can I Select Data from One table OR another Table? MYSQL-如何从第一个表返回数组,并在一个查询中使用该数组从另一个表中选择? - MYSQL - How to return array from first table and select from another table using that array in one query? 从一个表中获取不在另一个表中的所有数据 MySQL - Get all data from one table that's not in another table MySQL 从另一个mysql表中选择 - Select from another mysql table MySQL SELECT COUNT从一个表和ID从另一个表? - MySQL SELECT COUNT from one table and ID from another?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM