从两个以上的表中获取列,然后将它们插入到数组中

时间:2015-06-12 01:36:09

标签: php mysql arrays

我编写了一些代码来尝试从不同的表中获取多个列,并在将它们插入数组之前将它们连接在一起。它适用于我在单个表和两个不同的表上进行的操作,但是当我尝试为三个表执行此操作时,我突然得到的结果比我想象的要多。多很多。请看一下:

include('connect.php');
  

$ arrayX =" pieces.pieceID,playerDeck.amount,playerPieces.amount / pieces,   playerDeck,playerPieces /其中playerDeck.playerName =' playerName'和   playerPieces.playerName =' playerName'和pieces.name =   playerDeck.name = playerPieces.name&#34 ;;

$arrayX = explode('/', $arrayX);
$column = $arrayX[0];
$table = $arrayX[1];
$where = $arrayX[2];

$myArray = explode(',', $column);

global $connect;
$fetch = mysqli_query($connect,"SELECT $column FROM $table $where");
$count = mysqli_num_rows($fetch);

while($row=mysqli_fetch_array($fetch,MYSQLI_NUM)){

  $count --;
  $arrayCount = count($myArray);

  while($arrayCount > 0){
  $arrayCount--;
  $array[$count][$arrayCount]= $row[$arrayCount];
  }

}

$count = count($array);
echo $count." rows";

预期产出:

32 rows

实际输出:

31744 rows

31,744 / 32 = 922.这意味着我得到了922个我需要的行的相同副本。我不知道我做错了什么来获得922份副本,也不知道这实际上是怎么回事。如果有人能弄清楚我做错了什么,请指出。非常感谢你。

3 个答案:

答案 0 :(得分:1)

请运行以下查询:

SELECT COUNT(*) FROM
pieces p INNER JOIN playerDeck pd
ON p.name = pd.name
INNER JOIN playerPieces pp
ON pd.name = pp.name
WHERE pd.playerName = 'playerName' AND
pp.playerName = 'playerName'

如果您获得了31744行,您就会知道查询中的逻辑与您的预期不符。

答案 1 :(得分:0)

在看到c4pone的答案之后,在它被删除之前,我试图编辑它说它破坏它的部分?然后它奏效了。

$arrayX = "pieces.pieceID,playerDeck.amount,playerPieces.amount/pieces,playerDeck,playerPieces/where playerDeck.playerName = 'playerName' and playerPieces.playerName = 'playerName' and pieces.name = playerDeck.name and pieces.name = playerPieces.name";

答案 2 :(得分:0)

笛卡尔积?

您尝试使用“内部加入”编写查询。

就像那样:

>>> with eventlet.Timeout(5):
        time.sleep(6)
        x = 1
>>> x
1