简体   繁体   English

SQL如何根据另一个表构造一个表中两列的查询?

[英]SQL How to construct a query that two columns from one table depending on another table?

Please take a look at my table structure as below: 请看下面的表结构如下:

在此输入图像描述

The data in Area_PrimaryTable and Area_SecondaryTable will be mapped to Table_TableID . Area_PrimaryTableArea_SecondaryTable中的数据将映射到Table_TableID I'm wondering how to get the status ID for both table 100 and 111 when I'm querying AreaID = A101? 我想知道当我查询AreaID = A101时如何获取表100和111的状态ID?

Select * from Area where AreaID = A101

UPDATES: I've found my way to build such query but not sure if there is any better way? 更新:我找到了构建此类查询的方法,但不确定是否有更好的方法?

Please advise as below: 请指教如下:

Select * from Table where TableID in
(
    Select PrimaryTable from Area where AreaID = 'A101'
    union
    Select SecondaryTable from Area where AreaID = 'A101'      
)

That is the kind of example where JOIN s are especially useful. 这是JOIN特别有用的例子。 Look that up, since JOIN s are something that ends up being used very, very often when working with SQL. 仔细看看,因为JOIN最终会在使用SQL时非常常用。 They basically allow you to get information from several related tables as part of the same record. 它们基本上允许您从几个相关表中获取信息,作为同一记录的一部分。

So there you want to get information from two rows of Table, their relationship being that they are linked to the same row in Area. 因此,您希望从两行Table获取信息,它们的关系是它们链接到Area中的同一行。 And I will consider you just want to get both status. 我会认为你只想获得两种身份。 That can be solved with the following code : 这可以通过以下代码解决:

SELECT t1.Status, t2.Status
FROM   Area AS a
  JOIN Table AS t1 ON t1.TableID = a.PrimaryTable
  JOIN Table AS t2 ON t2.TableId = a.SecondaryTable
WHERE AreaID = 'A101'

Note that while using SELECT * is OK in my book when one experiments, I believe it should be avoided in production code in favour of explicitely naming the columns you want to get information from. 请注意,虽然在我的书中使用SELECT *时可以进行实验,但我相信在生产代码中应该避免使用它,而是明确地命名要从中获取信息的列。

Edit: despite the apparent differences, note that my code proposal has the same behaviour as Xtoxico's. 编辑:尽管存在明显的差异,但请注意我的代码提案与Xtoxico的行为相同。 Both proposals are equivalent as far as I know. 据我所知,这两个提案都是等同的。

Try with this.... 试试这个....

SELECT
    t1.status as StatusPrimary, t2.status as StatusSecondary 
FROM
    Area as a, 
    table as t1, 
    table as t2 
WHERE 
    a.primaryTable=t1.TableID and 
    a.secondaryTable=t2.TableId and 
    a.AreaID = A101;

Your Sample return two rows, and my code return in the same row the information 你的Sample返回两行,我的代码在同一行返回信息

I suggest this one: 我建议这个:

select t.* from table t, area a
where a.AreaID = 'A101' 
and t.TableID in (a.PrimaryTable, a.SecondaryTable)

Try this one 试试这个吧

SELECT a.AreaID, t.TableID, t.TableName, t.Status FROM table1 t
INNER JOIN (
    Select PrimaryTable, areaId from Area
    union
    Select SecondaryTable, areaID from Area
) a ON t.TableID = a.PrimaryTable order by a.AreaID

Output will look like this (base on your table's structure) 输出将如下所示(基于表的结构)

在此输入图像描述

I hope this is the answer what you looking for, if you only need find Area A101 , you can simply add WHERE AreaID = 'A101' after a ON t.TableID = a.PrimaryTable . 我希望这就是你要找的答案,如果你只需要找到A101区域,你可以在a ON t.TableID = a.PrimaryTable之后添加WHERE AreaID = 'A101' a ON t.TableID = a.PrimaryTable

EDIT : Sorry missed the point, maybe the query should like this : 编辑:抱歉错过了这一点,也许查询应该是这样的:

SELECT a.AreaID,
   MAX(IF(a.PrimaryTable = t.TableID, t.Status, null)) as T1,
   MAX(IF(a.SecondaryTable = t.TableID, t.status,null)) as T2
FROM Table1 t INNER JOIN Area a ON a.PrimaryTable = t.TableID OR a.SecondaryTable = t.TableID
GROUP BY AreaID

暂无
暂无

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

相关问题 SQL查询将两列与另一张表中的一列进行比较(并获得两个值) - SQL Query to compare two columns with one column from another table (and get two values) SELECT * FROM TABLE, TABLE 取决于另一个查询 - SQL - SELECT * FROM TABLE, TABLE depending on another query - SQL 将两列从一个表复制到另一个表 - Copying two columns from one table to another 如何构造一个SQL查询以从同一表的不同行中获取不同的列? - How to construct a SQL query to fetch different columns from same table in different rows? 如何从 SQL 表查询中的两列创建 Python 字典? - How to create a Python dictionary from two columns in a SQL table query? SQL-将一个表中多列的两行匹配到另一表中的1行。 - SQL - Matching two rows from multiple columns in one table to 1 row in another table. SQL查询从表的某些列复制到另一表的一列 - SQL query to copy from some columns of a table to one column in another table MySQL SQL命令从一个表中查询三个相同的列到另一个表 - MySQL SQL command to query from one table with three same columns to another table 如何将SQL表中的2列连接到另一个SQL表中的一列? - How to join 2 columns in a SQL table to one column in another SQL table? 如何将一个表中的两个或更多列与另一个表中的一个列联接在一起,即使第一个表列中的值为NULL - How to join two or more columns from one table with one column from another table, even there are NULL values in first table columns
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM