mysql结果的长度

时间:2011-10-05 12:35:46

标签: mysql

我提出了这个问题,我无法弄清楚为什么第37行 chal_rews.rewards 字段中的字符数会返回正好超过400个字符的341个字符。

SELECT
GROUP_CONCAT( users.email ) AS group_emails,
GROUP_CONCAT( 
CONCAT(
IFNULL(users.first_name,users.email),' ',IFNULL(users.last_name,''),'|',
IFNULL(users.facebook_id,''),'|',
IFNULL(users.email,''),'|',
users.id,'|',
invitations.wined,'|',
invitations.accepted,'|',
invitations.id
)
) AS participants,
owner.id AS owner_id,
owner.email,
owner.facebook_id,
owner.access_token,
owner.first_name,
CONCAT(  IFNULL(owner.first_name,owner.email),' ',IFNULL(owner.last_name,'')  )  AS owner_fullname,
challenges.id AS challenge_id,
challenges.challenge_type_id,
challenges.period_id,
challenges.period_amount,
challenges.category_item_id,
challenges.metric_amount,
challenges.owner_user_id,
CASE
WHEN challenges.status=2 THEN 'complete'
WHEN ( challenges.status=1 AND challenges.end_date<NOW() ) THEN 'waiting_for_winner'
ELSE challenges.status
END AS status,
DATE_FORMAT(challenges.start_date,'%d %M %Y @ %H:%i') AS start_date,
DATE_FORMAT(challenges.end_date,'%d %M %Y @ %H:%i') AS end_date,
DATE_FORMAT(challenges.created_at,'%d %M %Y @ %H:%i') AS created_at,
DATE_FORMAT(challenges.updated_at,'%d %M %Y @ %H:%i') AS updated_at,
challenges.inform,
chal_rews.rewards,
chal_rews.reward_names AS reward_name,
category_items.item_name,
LOWER( CONCAT(fixed_verb,' ',IF(metric_amount=-1,'',CONCAT(metric_amount,' ')),fixed_metric,' ',fixed_text,' ',challenges.period_amount ,' ',periods.name) ) AS challenge_string
FROM
invitations
Left Join users ON invitations.user_id = users.id
Left Join challenges ON invitations.challenge_id = challenges.id
Left Join users AS owner ON owner.id = challenges.owner_user_id
Left Join challenge_types ON challenges.challenge_type_id = challenge_types.id
Left Join periods ON challenges.period_id = periods.id
Left Join category_items ON challenges.category_item_id = category_items.id
Left Join (

SELECT
chal_rews.id,
chal_rews.challenge_id,
chal_rews.reward_item_id,
chal_rews.user_id,
chal_rews.created_at,
chal_rews.updated_at,
GROUP_CONCAT(CONCAT(
IFNULL(users.first_name,users.email),' ',IFNULL(users.last_name,''),'|',
IFNULL(users.facebook_id,''),'|',
IFNULL(users.access_token,''),'|',
IFNULL(users.email,''),'|',
users.id,'|',
IFNULL(chal_rews.id,' '),'|',
IFNULL(chal_rews.reward_item_id,' '),'|',
IFNULL(reward_items.name,' ')
)) AS rewards,
GROUP_CONCAT(reward_items.name) AS reward_names
FROM
chal_rews
Left Join users ON users.id = chal_rews.user_id
Left Join reward_items ON reward_items.id = chal_rews.reward_item_id
GROUP BY
chal_rews.challenge_id

) AS chal_rews ON challenges.id = chal_rews.challenge_id
WHERE 1=1

虽然如果我只运行这部分查询,那么奖励字段将返回它应该完全应该的内容,即完全连接的字符串。

 SELECT
    chal_rews.id,
    chal_rews.challenge_id,
    chal_rews.reward_item_id,
    chal_rews.user_id,
    chal_rews.created_at,
    chal_rews.updated_at,
    GROUP_CONCAT(CONCAT(
    IFNULL(users.first_name,users.email),' ',IFNULL(users.last_name,''),'|',
    IFNULL(users.facebook_id,''),'|',
    IFNULL(users.access_token,''),'|',
    IFNULL(users.email,''),'|',
    users.id,'|',
    IFNULL(chal_rews.id,' '),'|',
    IFNULL(chal_rews.reward_item_id,' '),'|',
    IFNULL(reward_items.name,' ')
    )) AS rewards,
    GROUP_CONCAT(reward_items.name) AS reward_names
    FROM
    chal_rews
    Left Join users ON users.id = chal_rews.user_id
    Left Join reward_items ON reward_items.id = chal_rews.reward_item_id
    GROUP BY
    chal_rews.challenge_id

我无法弄清楚出了什么问题。 SOS PLZ ......

1 个答案:

答案 0 :(得分:0)

这可能有点帮助。如果没有那么,对不起!你可以通过收紧chal_rews子选择结尾处的group by子句来获得一些乐趣。因此:

SELECT
chal_rews.id,
chal_rews.challenge_id,
chal_rews.reward_item_id,
chal_rews.user_id,
chal_rews.created_at,
chal_rews.updated_at,
GROUP_CONCAT(CONCAT(
IFNULL(users.first_name,users.email),' ',IFNULL(users.last_name,''),'|',
IFNULL(users.facebook_id,''),'|',
IFNULL(users.access_token,''),'|',
IFNULL(users.email,''),'|',
users.id,'|',
IFNULL(chal_rews.id,' '),'|',
IFNULL(chal_rews.reward_item_id,' '),'|',
IFNULL(reward_items.name,' ')
)) AS rewards,
GROUP_CONCAT(reward_items.name) AS reward_names
FROM
chal_rews
Left Join users ON users.id = chal_rews.user_id
Left Join reward_items ON reward_items.id = chal_rews.reward_item_id
GROUP BY
chal_rews.id,
chal_rews.challenge_id,
chal_rews.reward_item_id,
chal_rews.user_id,
chal_rews.created_at,
chal_rews.updated_at

如果没有我面前的实际表格和数据,确实有点棘手,但我认为如果你这样做,你会在奖励领域获得更一致的结果。我认为,当您将其作为主查询的一部分运行时,与在单独运行它时相比,在子选择中返回的列是不一致的。我怀疑这是因为让MySQL'选择'chal_rews.user_id和chal_rews.reward_item_id的值(后者又用于外连接到用户和reward_items表 - 因此带回不一致的数据)。更正式地说,当我从SELECT中排除GROUP BY中的非聚合列时,我认为你得到了一个不确定的结果集。

祝你好运并发布你如何上场!