簡體   English   中英

使用第一個元素的值獲取結果集中第二個和第三個元素的值

[英]get values of 2nd and 3rd element of a result set using the value of the 1st element

acur.execute("Select id,firstname,lastname from tbl_emp")
tk = acur.fetchall()

結果集是:

tk = [(1,'a','b'),(2,'c','d'),(3,'e','f')]

如何使用第一個元素的值搜索此結果集?

說我使用2搜索,結果將為c和d。

如果您只是在瀏覽元組:

>>> def search(_list, _value):
...     for item in _list:
...             if item[0] == _value:
...                     return item[1], item[2]
... 
>>> search(tk, 2)
('c', 'd')
>>> search(tk, 3)
('e', 'f')
>>> search(tk, 4)
>>> 

為什么不首先過濾它而不是獲取所有記錄:

acur.execute("""
    SELECT 
        firstname, 
        lastname 
    FROM 
        tbl_emp 
    WHERE 
        id = %s""", (2, ))
firstname, lastname = acur.fetchone()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM