我写过这个问题:
select
poll_choice_translations.display as label,
sum(users_to_polls_to_votes.votes) as value,
users.zipcode
from
(select
max(id) as id,
user_id,
poll_id from users_to_polls
group by user_id, poll_id) userPoll
left join users_to_polls_to_votes
on userPoll.id = users_to_polls_to_votes.users_to_polls_id
left join poll_choice_translations
on users_to_polls_to_votes.choice_id = poll_choice_translations.poll_choice_id
left join users
on userPoll.user_id = users.id
left join poll_choices
on poll_choice_translations.poll_choice_id = poll_choices.id
where
userPoll.poll_id = 51 AND
poll_choice_translations.language_id = 1
group by
users.zipcode,
poll_choice_translations.display,
poll_choices.order
order by
poll_choices.order
我在一个非常小的测试数据库上运行它,并得到我的结果:
zipcode | label | value
10001 | yes | 1
10002 | yes | 5
10002 | no | 1
在Access中,我可以交叉选择它以获得一组结果,如:
zip code | yes | no
10001 | 1 | 0
10002 | 5 | 1
我正试图找出使用MySQL和/或PHP执行此操作的方法。我想我已经看到了一些例子,其中列名称(即 - 是/否)是硬编码的,但我需要它是动态的 - 一些结果将有2种不同类型的标签,其他将有3,4或5
有人可以帮忙吗?我以前在Access方面相当不错,但我仍然在MySQL世界中沾沾自喜。
谢谢!
这是来自/ r / phphelp的交叉帖子