3

我最后的作品之一是基于 Yii 的硬件目录。每个项目可以与很多组链接。

CREATE TABLE item_group (
id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
itemId INT(10) UNSIGNED NOT NULL,
groupId INT(10) UNSIGNED NOT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

只需要显示用户选择的所有 groupIds 的 itemIds。这是我的错误解决方案:

$groups = isset($_GET['groups']) ? array_merge(array_diff($_GET['groups'], array('0'=>'-')),array()) : array();
$sql = '';
$brackets = '';
$groupMaxKey = count($groups) - 1;
//some code here
for($i=0;$i<=$groupMaxKey;$i++){
    $sql .= "SELECT itemId FROM item_group WHERE groupId='".$groups[$i]."' ";
    if($i != $groupMaxKey){
        $sql .= "AND itemId IN (";
        $brackets .= ")";
    } else {
        $sql .= $brackets;
    }
}

我发现嵌套的最大级别是 32。更多的组会出错。什么是最干净的解决方案?

在这里澄清一个查询示例:

SELECT itemId FROM item_group 
WHERE groupId='31' AND itemId IN (
    SELECT itemId FROM item_group 
    WHERE groupId='24' AND itemId IN (
        SELECT itemId FROM item_group 
        WHERE groupId='35'
    )
)

//// 答案确实有效:

SELECT g1.itemId 
FROM ((((((((((((((((((((((((((((((((((((((( item_group g1 
INNER JOIN item_group g2 ON g1.itemId = g2.itemId)
INNER JOIN item_group g3 ON g1.itemId = g3.itemId)
INNER JOIN item_group g4 ON g1.itemId = g4.itemId)
INNER JOIN item_group g5 ON g1.itemId = g5.itemId)
INNER JOIN item_group g6 ON g1.itemId = g6.itemId)
INNER JOIN item_group g7 ON g1.itemId = g7.itemId)
INNER JOIN item_group g8 ON g1.itemId = g8.itemId)
INNER JOIN item_group g9 ON g1.itemId = g9.itemId)
INNER JOIN item_group g10 ON g1.itemId = g10.itemId)
INNER JOIN item_group g11 ON g1.itemId = g11.itemId)
INNER JOIN item_group g12 ON g1.itemId = g12.itemId)
INNER JOIN item_group g13 ON g1.itemId = g13.itemId)
INNER JOIN item_group g14 ON g1.itemId = g14.itemId)
INNER JOIN item_group g15 ON g1.itemId = g15.itemId)
INNER JOIN item_group g16 ON g1.itemId = g16.itemId)
INNER JOIN item_group g17 ON g1.itemId = g17.itemId)
INNER JOIN item_group g18 ON g1.itemId = g18.itemId)
INNER JOIN item_group g19 ON g1.itemId = g19.itemId)
INNER JOIN item_group g20 ON g1.itemId = g20.itemId)
INNER JOIN item_group g21 ON g1.itemId = g21.itemId)
INNER JOIN item_group g22 ON g1.itemId = g22.itemId)
INNER JOIN item_group g23 ON g1.itemId = g23.itemId)
INNER JOIN item_group g24 ON g1.itemId = g24.itemId)
INNER JOIN item_group g25 ON g1.itemId = g25.itemId)
INNER JOIN item_group g26 ON g1.itemId = g26.itemId)
INNER JOIN item_group g27 ON g1.itemId = g27.itemId)
INNER JOIN item_group g28 ON g1.itemId = g28.itemId)
INNER JOIN item_group g29 ON g1.itemId = g29.itemId)
INNER JOIN item_group g30 ON g1.itemId = g30.itemId)
INNER JOIN item_group g31 ON g1.itemId = g31.itemId)
INNER JOIN item_group g32 ON g1.itemId = g32.itemId)
INNER JOIN item_group g33 ON g1.itemId = g33.itemId)
INNER JOIN item_group g34 ON g1.itemId = g34.itemId)
INNER JOIN item_group g35 ON g1.itemId = g35.itemId)
INNER JOIN item_group g36 ON g1.itemId = g36.itemId)
INNER JOIN item_group g37 ON g1.itemId = g37.itemId)
INNER JOIN item_group g38 ON g1.itemId = g38.itemId)
INNER JOIN item_group g39 ON g1.itemId = g39.itemId)
INNER JOIN item_group g40 ON g1.itemId = g40.itemId)
WHERE g1.groupId='1' AND g2.groupId='2' AND g3.groupId='3' AND g4.groupId='4' AND g5.groupId='5' AND g6.groupId='6' AND g7.groupId='7' AND g8.groupId='8' AND g9.groupId='9' AND g10.groupId='10' AND g11.groupId='11' AND g12.groupId='12' AND g13.groupId='13' AND g14.groupId='14' AND g15.groupId='15' AND g16.groupId='16' AND g17.groupId='17' AND g18.groupId='18' AND g19.groupId='19' AND g20.groupId='20' AND g21.groupId='21' AND g22.groupId='22' AND g23.groupId='23' AND g24.groupId='24' AND g25.groupId='25' AND g26.groupId='26' AND g27.groupId='27' AND g28.groupId='28' AND g29.groupId='29' AND g30.groupId='30' AND g31.groupId='31' AND g32.groupId='32' AND g33.groupId='33' AND g34.groupId='34' AND g35.groupId='35' AND g36.groupId='36' AND g37.groupId='37' AND g38.groupId='38' AND g39.groupId='39' AND g40.groupId='40'
4

2 回答 2

4

您可以通过使用 INNER JOIN 来实现这一点。没有理由嵌套这些语句。

您的示例案例的正确查询的任何示例都是:

SELECT g1.itemId 
FROM (( item_group g1 
        INNER JOIN item_group g2 ON g1.itemId = g2.itemId)
        INNER JOIN item_group g3 ON g1.itemId = g3.itemId) 
WHERE g1.groupId='31' AND g2.groupId='24' AND g3.groupId='35'

我已经在一个包含三列的简单表上对此进行了测试(id, itemId, groupId),它可以工作。将这种语句放在循环中很容易,并且连接数没有最大值。

为了使这个运行更快,您应该索引表itemId中的列item_group

您可以使用以下 SQL 语句执行此操作:

ALTER TABLE item_group ADD INDEX ( itemId )
于 2012-07-20T03:29:35.043 回答
1

如果我正确理解了这个问题,您似乎正在尝试在不同组中找到 itemIds 的交集?

我们可以做的是计算我们正在查看的组中的所有 itemId...

SELECT itemId, COUNT(groupId) as CNT
FROM item_group
WHERE groupId IN (*GROUP_IDS*)
GROUP BY itemId

现在,只要一个项目不能在同一个组中两次,我们就可以从这个查询中拉出那些 CNT 等于我们正在查看的组数的行......

SELECT * FROM (
    SELECT itemId, COUNT(groupId) as CNT
    FROM item_group
    WHERE groupId IN (*GROUP_IDS*)
    GROUP BY itemId
) as TMP WHERE CNT = *NUMBER_OF_GROUP_IDS*

那应该这样做。

于 2012-07-20T03:20:29.397 回答