简体   繁体   English

MySQL,使用一个查询的值从另一个查询获取结果

[英]MySQL , use values from one query to get a result from another

I am an absolute newbie with MySQL and other than basic select functions I don't know much more! 我是MySQL的绝对新手,除了基本的select函数外,我不了解更多! I have been reading various posts about joining Queries etc But it's currently going over my head far too much to be able to build what I need and I'm short of time to learn! 我一直在阅读有关加入Queries等的各种帖子,但是目前我的头绪太长了,无法满足我的需求,而且我学习的时间很短!

So would anyone be able to pull me together a query to work out the below, please? 因此,有谁能将我的查询汇总起来以解决以下问题?

I have 3 tables which contain fields below that I need to use. 我有3个表格,其中包含以下需要使用的字段。

Accounts 帐目

i_account,
id,
i_env (this value would be =2),
first_usage_time

UA UA

i_ua,
mac,
inventory_id,
description

UA_Links UA_链接

i_ua,
i_account

So as you can see the table UA_Links contains the two fields that tie the UA and Accounts tables together. 如您所见,表UA_Links包含将UA和Accounts表联系在一起的两个字段。

What I need to do is, output from the Accounts table, id and first_usage along with the mac, inventory_id and description from the UA table. 我需要做的是,从Accounts表中输出id和first_usage以及从UA表中获取mac,inventory_id和描述。

Hopefully that makes sense? 希望这有意义吗?

Many thanks in advance Mike 预先非常感谢Mike

The most simple way of doing this is to create a simple join. 最简单的方法是创建一个简单的联接。

SELECT Account.id, Accounts.first_usage_time, UA.mac, UA.inventory_id, UA.description 
FROM Accounts, UA, UA_Links
WHERE Accounts.id = UA_Links.i_account 
AND UA.i_ua = UA_Links.i_ua

A little explanation: you can use a comma to select multiple tables. 一点解释:您可以使用逗号选择多个表。 If you want to select a column you need to use the syntax {tablename}.{column_name}. 如果要选择列,则需要使用语法{tablename}。{column_name}。 To connect the 3 tables together you need to link the ID's in the WHERE statement. 要将3个表连接在一起,您需要在WHERE语句中链接ID。

I'm not totally sure if I got the correct columns, you might want to check that (are UA.i_ua and UA_Links.i_ua the same?). 我不确定我是否获得了正确的列,您可能需要检查一下(UA.i_ua和UA_Links.i_ua是否相同?)。

May be this is can help..:) 也许这可以帮助..::)

select a.id,a.first_usage_time
from Accounts a
left join UA_Links b
on b.i_account=a.i_account
left join UA c
on c.i_ua=b.i_ua

This is a very simple join, all you need is to tell the query which field matches which of the other table: 这是一个非常简单的联接,您所需要做的就是告诉查询哪个字段与另一个表的哪个字段匹配:

select * from accounts as a inner join ua_links as l 
on a.i_account = l.i_account 
Inner join ua on l.i_ua = ua.i_ua

Hope this will work for your needs, 希望这能满足您的需求,

select acc.id as YOURALIAS, acc.first_usage_time as YOURALIAS, 
ua.mac as YOURALIAS, ua.inventory_id as YOURALIAS, 
ua.description as YOURALIAS 
from ua INNER JOIN ua_links ON ua.i_ua = ua_links.i_ua 
inner join accounts acc on ua_links.i_account = acc.i_account

It is a best practice to use aliases, you can find more on http://www.w3schools.com/sql/sql_alias.asp . 最佳做法是使用别名,您可以在http://www.w3schools.com/sql/sql_alias.asp上找到更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM