abhixec
abhixec

Reputation: 177

how to compare the rows of a table using python

Sorry if the title was misleading or not very descriptive, but here is what I want I have a table with 2 columns :

name|value
1.aaaa|132412
2.aaaa|234124
3.aaaa|542253
bbbb|234324
bbbb|342342

so I want to basically compare the rows where name="aaaa" the part where I am stuck is how do I compare all the rows with name="aaaa"

sql="select value from table where name='aaaa'"
cursor.execute(sql)
result=cursor.fetchall()
for row in result:
  value=row[0]

how do I go about from here ?

edit: I want to compare value of 1 with 2,1 with 3 and similarly 2 with 3 and so on..

Upvotes: 1

Views: 7360

Answers (1)

ernie
ernie

Reputation: 6356

You've figured out the code to get all the results, and then it sounds like you want to compare all the two-item combinations of values.

sql="select value from table where name='aaaa'"
cursor.execute(sql)
results=cursor.fetchall() # changed to results to better reflect the list structure
count = len(results)
for i in range(0, count):
  for j in range (i+1, count):
    print results[i][0], results[j][0]

That'll print all the pairs . . . obviously you'll want to parse them instead, and do your comparisons where the print statement is.

Upvotes: 2

Related Questions