如何根据owner_id连接两个表?

时间:2017-11-01 11:58:10

标签: mysql

我有两个表:owners_table和items_table。

Owners_table:

OwnerID,Name(this is the owner's name),Email

Items_table:

ItemID, OwnerID,Name(this is the item name),Location

我需要在datagridview中显示Itemid,ownerid,name(of owner),name(of item), location. 我在查询中需要一些帮助:)我想使用将连接两个表的ownerid加入表。谢谢!

5 个答案:

答案 0 :(得分:0)

将此查询与名称列的别名一起使用:

select
    O.OwnerID,
    I.Itemid,
    I.Name as ItemName,
    O.Name as OwnerName,
    I.Location
from
    Owners_table O
    inner join Items_table I on
        O.OwnerID = I.OwnerID

答案 1 :(得分:0)

您可以使用:

select I.Itemid, I.ownerid, I.name as ItemName, O.name as OwnerName, I.location from
Items_table I join Owners_table O on I.ownerid = O.ownerid

答案 2 :(得分:0)

countries = {
'United Kingdom',
'France',
'Germany',
'Spain',
'Portugal',
'Poland',
'Czech Republic',
'Belgium',
'Hungary',
'Sweden',
'Norway',
'Finland'

}

capitals = {
    'London',
    'Paris',
    'Berlin',
    'Madrid',
    'Lisbon',
    'Warsaw',
    'Prague',
    'Brussels',
    'Budapest',
    'Stockholm',
    'Oslo',
    'Helinski'

}

import csv
import random

ans = True

print("Press X to quit")

try:
        # a = append, w = write
    with open('capitals.csv', 'w', newline='') as csvfile:
        write = csv.writer(csvfile, dialect='excel')

    while ans:
        # Country Input
        country = input('What is the country?')
        if country != 'X':
            if country in countries:
                country_name = countries[country]
            else:
                country_name = 'Country is not listed.'
                print('The name of the country that you have chosen is', country)

            # Capital Input
            capital = input('What is the capital?')
            if capital != 'X':
                if capital in capitals:
                    capital_name = capitals[capital]
            else:
                capital_name = 'Capital is not listed.'
                print('The name of the capital that you have chosen is', capital)

                write.writerow([country, capital])

                def validmark():
                    global mark
                    if int(mark) >=0 and int(mark) <=10:
                        return True
                    else:
                        return False

    total = input('What is the maximum amount of marks? ')
    while not validmark(total):
        total = input('Re-enter maximum amount of marks ')

        if country =="X" or capital == "X":
            print("\nGoodbye") 
            ans = False

except OSError as err:
    print("Unable to access file")

答案 3 :(得分:0)

您必须根据表格更改已加入的列名称。

SELECT Items.itemid, Owners.ownerid, Owners.name, Items.name FROM Owners_table Owners join  Items_table Items on Items.OwnerId=Owners.id

答案 4 :(得分:0)

SELECT dbo.Owners_table.OwnerID,dbo.Items_table.ItemID,dbo.Items_table.Name AS ITEM_NAME,dbo.Owners_table.Name AS Owner_name,dbo.Items_table.Location 来自dbo.Items_table INNER JOIN   dbo.Owners_table ON dbo.Items_table.OwnerID = dbo.Owners_table.OwnerID