mysql查询爆炸与两个表连接

时间:2017-03-06 21:56:15

标签: php mysql implode

我的SQL查询

SELECT 
    team.id,
    team.atitle,
    team.etitle,
    team.cup_id,
    team.logo,
    team.modarb,
    team.count_id,
    team.link AS tlink,
    country.atitle AS name,
    country.etitle AS ename,
    cup.id AS catid,
    cup.link,
    cup.description,
    cup.name AS cupname
    FROM cup LEFT JOIN team ON (cup.id IN (". implode(', ', 'team.cup_id') ."))
    LEFT JOIN country ON (country.id = team.count_id)
    where cup.id='5'

row team.cup_id看起来像这个5,4,3, 需要知道如何在mysql查询中使用implode

1 个答案:

答案 0 :(得分:0)

您应该修复数据结构,这样就不会将ID列表存储为字符串。这样做是错误的,错误的,错误的:

  • 将数字存储为字符串是错误的。
  • 如果没有正确声明的外键引用,则表中的id是错误的。
  • 当SQL提供了一种存储列表的好方法时,将列表存储在单个列中是错误的:表。

万一你无法修复数据结构,你可以这样做:

FROM cup LEFT JOIN
     team
     ON find_in_set(cup.id, team.cup_id) > 0 LEFT JOIN
     country
     ON (country.id = team.count_id)

我只提供此作为止损,直到您将数据修复为表格中的SQLish。