如何连接两个表,但包括所有行(即使为空白或null)?

时间:2018-08-02 11:44:19

标签: mysql

我有两个桌子。我想将它们合并,但包括不返回值的行。这个问题可能会被标记为重复或类似内容,但是我已经尝试阅读其他帖子,但仍然失败。因此,我可能低于普通的MySQL程序员。希望有人能帮忙。

table_price_list

item_id price_type_id   price_amount
1       1               100.00
1       2               95.00
1       3               90.00
1       4               85.00
1       5               80.00
1       6               75.00
2       1               201.56
2       2               196.45
2       3               191.78
2       4               186.36
3       1               1210.12
3       2               1205.45
3       3               1200.69
3       4               1195.48
3       5               1190.98

table_price_type

price_type_id   price_type
1               srp
2               reseller
3               distributor
4               mega
5               depot
6               special

所需的输出

item_id price_type_id   price_type
1       srp             100.00
1       reseller        95.00
1       distributor     90.00
1       mega            85.00
1       depot           80.00
1       special         75.00
2       srp             201.56
2       reseller        196.45
2       distributor     191.78
2       mega            186.36
2       depot           null
2       special         null
3       srp             1210.12
3       reseller        1205.45
3       distributor     1200.69
3       mega            1195.48
3       depot           1190.98
3       special         null

到目前为止,我能得到的最好的是,这将空白price_type

 select b.item_id, a.price_type, b.price_amount
    from table_price_type A
    left outer join table_price_list B on A.price_type_id=B.price_type_id

它不必为null,它可以只是空白('')。

1 个答案:

答案 0 :(得分:0)

您可以使用左联接来获得所需的结果。

def intercept(callback):
    def wrapper(*args, **kwargs):
        # pass callback, args and kwargs to the thread
        pass
    return wrapper

@intercept
def do_some_work(first, second, third=None):
    time.sleep(10)

def bg_thread():
    while True:
        # acquire callback, args and kwargs from intercept decorator
        # if new job is scheduled create a process and execute it
        # if process did not finish in timeout, terminate it
        p = multiprocessing.Pool()
        ret = p.apply_async(callback, args, kwargs)
        p.close()
        try:
            ret.get(5)
        except:
            p.terminate()


t = threading.Thread(target=bg_thread)
t.start()
do_some_work()