MySQL在这个声明中的位置?

时间:2011-02-16 19:20:10

标签: php mysql sql join

这有点超出了我的技能,我从SO的优秀人员那里获得了很多帮助。我现在需要的是放入一个MATCH()... AGAINST()但我不知道在哪里插入它?

我的查询是(这是简短版本):

SELECT
  SQL_CALC_FOUND_ROWS i.idItems RowCount,
  i.* Items,
  # Create a JSON formatted field
  CONCAT('{',GROUP_CONCAT('"',Attributes.key, '":"', CONVERT(Attributes.value,CHAR),'"'),'}') as Attributes,
  IF (te.Key IS NULL,tp.Key,te.Key) as Type,
  tc.Value Color,
  l.* Location,
  c.Name,
  c.Mobile,
  c.Mail
FROM
  (SELECT ItemID, ats.Key, ats.Value FROM attributeStrings as ats
   UNION ALL
   SELECT ItemID, ati.Key, ati.Value FROM attributeIntegers as ati
  ) Attributes
JOIN Items i ON
  i.idItems = Attributes.ItemID
  AND CheckIn >= DATE_SUB('2011-02-16 00:00:00',INTERVAL 90 DAY)
  AND CheckIn <= DATE_ADD('2011-02-16 23:59:59',INTERVAL 90 DAY)
  AND Checkout IS NULL
LEFT JOIN Customers c ON c.idCustomers = i.CustomerID
LEFT JOIN attributeintegers atli ON atli.itemid = i.idItems AND atli.key = 'Location'
LEFT JOIN locations l ON l.id = atli.value
LEFT JOIN attributestrings atts ON atts.itemid = i.idItems AND atts.key = 'Type' LEFT
JOIN Lists tp ON tp.value = atts.value
LEFT JOIN attributestrings attes ON attes.itemid = i.idItems AND attes.key = 'Tech' LEFT
JOIN Lists te ON te.value = attes.value
LEFT JOIN attributeintegers atci ON atci.itemid = i.idItems AND atci.key = 'Color' LEFT
JOIN Strings tc ON tc.StringID = atci.value
GROUP BY Attributes.ItemID
ORDER BY CheckIn DESC

现在我需要在这里的某处获得此声明

MATCH(attributestrings.Value) AGAINST("Nokia" IN BOOLEAN MODE)

正如您所看到的,有一个名为 attributestrings 的表,它有3列: ItemID ,* Key *和 Value 。我需要在列中搜索 AGAINST()中的字词,并且只显示与此匹配的结果以及其他标准,例如上面的日期和结帐。

我尝试在AND Checkout IS NULL之后添加声明:

AND Checkout IS NULL
AND MATCH(Attributes.Value) AGAINST("Nokia" IN BOOLEAN MODE)

我不得不使用 Attributes.Value 而不是 attributestrings ,因为它没有找到该表。这只导致CONCATENATED列属性仅包含值“Nokia”,即使还有更多要加强的地方。

我希望有人愿意接受这个挑战......

//坦克你。

[编辑]

我试图在蒂姆·富尔兹(Tim Fultz)消化之前把它放在GROUP之前,但是我得到了错误 'Where子句'中的未知列' attributestrings.Value '

LEFT JOIN attributeintegers atci ON atci.itemid = i.idItems AND atci.key = 'Color' LEFT JOIN Strings tc ON tc.StringID = atci.value
WHERE MATCH(attributestrings.Value) AGAINST("Nokia Sony" IN BOOLEAN MODE)
GROUP BY Attributes.ItemID

3 个答案:

答案 0 :(得分:0)

通常将其放在Where子句中:

WHERE MATCH(attributestrings.Value) AGAINST("Nokia" IN BOOLEAN MODE)

答案 1 :(得分:0)

我想我想出了一个解决方案......

我又添了一个:

LEFT JOIN attributestrings fts ON fts.itemid = i.idItems AND MATCH(fts.Value) AGAINST("Nokia Sony" IN BOOLEAN MODE)

GROUP BY...之前,然后在主select语句中包含fts.Value ftsv。现在我可以在HAVING ftsv IS NOT NULL之间插入GROUP BY..。和ORDER BY...

这给了我想要的结果,但现在查询开始有点慢......

答案 2 :(得分:0)

当您在查询指定名称的每个位置分配表名时,查询中存在问题。就像你给了属性串一样ats现在在任何地方都使用ats,这样就可以了。