带附加列的SQL连接查询

时间:2019-03-26 05:10:29

标签: sql-server

我有以下SQL Server表:

资产表

+---------+-----------+------------+
| AssetID | AssetName | LocationID |
+---------+-----------+------------+

位置表

+-------------+---------------+------------------+
|  LocationID |  LocationName |  SubLocationName |
+-------------+---------------+------------------+

如何返回结果,其中资产表LocationID =位置表LocationID,还要附加相应的 将LocationName和SubLocationName作为额外的列?

资产表

+---------+-----------+------------+
| AssetID | AssetName | LocationID |
+---------+-----------+------------+
|       1 | Asset1    |        123 |
+---------+-----------+------------+

位置表

+------------+--------------------+----------------------+
| LocationID | LocationName       | SubLocationName      |
+------------+--------------------+----------------------+
|        123 | Area1              | Sub1                 |
+------------+--------------------+----------------------+

将返回:

+--------+-------+------+
| Asset1 | Area1 | Sub1 |
+--------+-------+------+

谢谢 保罗。

2 个答案:

答案 0 :(得分:1)

您需要连接表并查询您感兴趣的列:

SELECT AssetName, LocationName, SubLocationName
FROM   Assets a
JOIN   Location l ON a.LocationId = l.LocationId

答案 1 :(得分:0)

def get_subject(msgobj) :
    subject = None
    if msgobj['Subject'] is not None:
        decodefrag = decode_header(msgobj['Subject'])
        subj_fragments = []
        for s , enc in decodefrag:
            if enc:
                s = unicode(s , enc).encode('utf8','replace')
            subj_fragments.append(s)
        subject = ''.join(subj_fragments)
        subject = re.sub('\n', '', subject)
    return subject

def get_msg_file_as_attachment(message_part):
    attachment = {}
    attachment['data'] = message_part.get_payload()[0].as_string(unixfrom=True)
    attachment['content_type'] = message_part.get_content_type()
    attachment['name'] = get_subject(message_part.get_payload()[0])
    attachment['name'] +=  '.eml'
    attachment['size'] = len(attachment['data'])

    return attachment

def parse_attachment(message_part):
    content_disposition = message_part.get("Content-Disposition", None)
    content_type = message_part.get_content_type()
    if content_disposition:
        dispositions = content_disposition.strip().split(";")
        if bool(content_disposition and (dispositions[0].lower() == "attachment" or dispositions[0].lower() == "inline")):
            if (content_type.lower().strip() == 'message/rfc822'):
                return get_msg_file_as_attachment(message_part)

            else:
                file_data = message_part.get_payload(decode=True)
                attachment = {}
                attachment['data'] = file_data
                attachment['content_type'] = content_type
                attachment['size'] = len(file_data)
                attachment['name'] = message_part.get_filename()
                return attachment

    return None