dongwei1895 2016-08-14 20:20
浏览 36
已采纳

在所有组合sql中查询6个不同的列,其中包含6个数字

I have a database with 6 columns, n1 - n6. each has a number in it, so each row has 6 different numbers. When I query I am checking the 6 numbers I have to submit against each row in the database. I need the query to change the combination so in theory 36 queries are done.

So an example dataset might be

N1  N2  N3  N4  N5  N6
1   12  54  36  17  23

and the numbers I have to query against this would be

1 54 36 17 23 12

so they are the same but because they are in a different order it would not return a result. All 6 must match to return positive.

Example query:

$sql = "SELECT * FROM numbers WHERE n1 = :n1 AND n2 = :n2 AND n3 = :n3 AND n4 = :n4 AND n5 = :n5 AND n6 = :n6";
$stmt = $conn->prepare($sql);
$stmt->bindParam(":n1", $n1);
$stmt->bindParam(":n2", $n2);
$stmt->bindParam(":n3", $n3);
$stmt->bindParam(":n4", $n4);
$stmt->bindParam(":n5", $n5);
$stmt->bindParam(":n6", $n6);
$stmt->execute();

I could do this with 36 different queries, but to be able to do it in one would be much more usable.

  • 写回答

4条回答 默认 最新

  • duandong7980 2016-08-14 20:51
    关注

    Hmmm . . . Storing six numbers in a row sounds like a bad data format. If these were rows instead of columns, then it would be easier.

    However, assuming no duplicates, you can do:

    SELECT *
    FROM numbers
    WHERE n1 IN (:n1, :n2, :n3, :n4, :n5, :n6) AND
          n2 IN (:n1, :n2, :n3, :n4, :n5, :n6) AND
          n3 IN (:n1, :n2, :n3, :n4, :n5, :n6) AND
          n4 IN (:n1, :n2, :n3, :n4, :n5, :n6) AND
          n5 IN (:n1, :n2, :n3, :n4, :n5, :n6) AND
          n6 IN (:n1, :n2, :n3, :n4, :n5, :n6);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题