开发者

length of mysql result

开发者 https://www.devze.com 2023-04-11 02:37 出处:网络
I made this query and i cant figure out why number of characters in chal_rews.rewards field on line 37 returns exactly 341 characters where there are more than 400 characters.

I made this query and i cant figure out why number of characters in chal_rews.rewards field on line 37 returns exactly 341 characters where there are more than 400 characters.

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.i开发者_Python百科d
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

while if i run only this part of query then rewards field returns exactly what it should i.e. full concatenated string.

 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

I cant figure out what is wrong. SOS plz...


Bit of a punt this but it might help. If it doesn't then, well, sorry! You might get some joy by tightening up the group by clause at the end of the chal_rews sub-select. Hence:

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

It's a little tricky to be sure without the actual tables and data in front of me but I think you will get a more consistent result in the rewards field if you do this. I think the columns that are coming back in the sub-select are inconsistent when you run it as part of the main query compared to when you run it in isolation. I suspect this is being caused by letting MySQL 'choose' a value for the chal_rews.user_id and chal_rews.reward_item_id (which in turn are used to outer join onto the users and reward_items tables - hence bringing back inconsistent data). More formally I reckon you are getting an indeterminate results set when excluding non-aggregated columns from the GROUP BY that are present in the SELECT.

Good luck and post how you get on!

0

精彩评论

暂无评论...
验证码 换一张
取 消