您不知道的匹配记录存在

时间:2015-12-31 04:47:50

标签: python mysql

我有一个包含多个列的表,我需要找到一种方法来查看该列中有多少行匹配,因为我需要每个匹配的项具有相同的值,然后转到下一个值。

mysql> select name,Location from relays
    -> ;
+----------+----------+
| name     | Location |
+----------+----------+
| Relay 1  | 3        |
| Relay 2  | 3        |
| Relay 3  | 3        |
| Relay 4  | 3        |
| Relay 5  | 3        |
| Relay 6  | 4        |
| Relay 7  | 6        |
| Relay 8  | 7        |
| Relay 9  | 9        |
| Relay 10 | 8        |
+----------+----------+
10 rows in set (0.00 sec)

我知道我可以进行选择查询并使用select的where部分,只需要求数字3,但我希望能够在不必将其放入代码中的情况下执行此操作。

这是我现在所拥有的,所以我可以看到它有效,所以我在其中有印刷声明。

a = 1
cursor.execute("SELECT Location FROM relays")
rows = cursor.fetchall()
for row in rows:
   print row[0]

   if (row[0] == '3'):
      print "temp"+str(a)
   else:
      a = a+1
      print "temp"+str(a)

它打印出我需要的内容,但我不想将数字3放在if语句中。

这是我得到的......

3  temp1
3  temp1
3  temp1
3  temp1
3  temp1
4  temp2
6  temp3
7  temp4
9  temp5
8  temp6

我正在构建一个配置文件,我将使用它完全打印出来,但将被放入我正在创建的函数中。

如果我添加继电器11并将其放入位置6,那么我希望它打印出来..

3  temp1
3  temp1
3  temp1
3  temp1
3  temp1
4  temp2
6  temp3
7  temp4
9  temp5
8  temp6
6  temp3

我尝试使用计数器a代替3,但它没有得到正确的答案,它只是使所有的临时值增加1。

0 个答案:

没有答案