-2
A
답변
1
대답
권장되는 솔루션은 모든 계산 된 정보를 제공하는 SQL 문을 통해 그룹을 사용하는 것입니다.
$result = mysql_query('select sum(value) as positivecount, count(*) as total, user_id from answers group by user_id');
while($data = mysql_fetch_assoc($result)){
// $data will hold positivecount,total, and user_id giving you all the data you need for calculating negative answer values.
}
// alternatively, use a query like this for counting the answers that were 'beans':
// select sum(if(value = "beans", 1, 0)) as answered_beans, count(*) as total, user_id from answers group by user_id
참조 : http://dev.mysql.com/tech-resources/articles/wizard/page3.html
1
실제로 두 SQL 테이블을 가지고이 문제에 대한 가장 우아한 솔루션입니다; 하나는 각 사용자에 대해 하나의 행 (사용자 ID, 사용자 이름 등)이고 하나는 각 투표에 대해 하나의 사용자 당 여러 명이 될 수 있습니다.
다음 예는 데이터에 대한 일부 정보를 표시합니다. 당신이 배열하면 다음 부정적인 계산하는 데 사용할 수있는 사용자별로 사용자와 전체 답변 당 긍정적 인 답변을 잡고 것이다
$resultq = mysql_query('select value, user_id from answers');
$answers_per_user = array(); // positive answers per user
$totals_per_user = array(); // total answers per user
while($result = mysql_fetch_assoc($resultq)){
if($result['answer'])
$answers_per_user[$result['user_id']] += $result['answer']; // increment positive answer counter for user
$totals_per_user[$result['user_id']]++;
}
: 루프를 사용하여 간단한 (권장하지 않음) 솔루션이
<?php
$sqlusers = mysql_query("SELECT userid FROM user_table")//This line grabs all users from the database.
$users = mysql_fetch_array($sqlusers);//This line creates an array containing all users.
foreach($users as $key=>$currentuser){
$sqlvotes = mysql_query("SELECT userid, vote FROM vote_table WHERE userid = $currentuser[userid]");
$votes = mysql_fetch_array($sqlvotes);//obtain an array of votes the current user has submitted
$votefrequency = array_count_values($votes)//counts the amount of trues and falses in the $votes array, and returns an array with the [true] and [false] indexes containing their respective frequency.
echo "user ".$userid." has voted ".$votefrequency[true]." times true and ".$votefrequency[false]." times false/n";
echo "average vote:". (($votefrequency[true] - $votefrequency[false] > 0) ? "true" : "false");
}
관련 문제
- 1. Excel : 참 또는 거짓을 반환하는 VLOOKUP?
- 2. 참 및 거짓을 나타내는 GLboolean 값은 무엇입니까?
- 3. Strstr이 참 대신 거짓을 반환 함
- 4. 자바의 환경 설정에서 참 거짓을 변경하십시오.
- 5. JQuery와 : 반환 참 또는 거짓
- 6. 참 또는 1 동안?
- 7. Postgresql : 참 OR 절의 수로 랭크를 계산하십시오.
- 8. 배열의 변수 : 참 또는 거짓
- 9. php - 참 또는 참 차이가 있습니까? 난 그냥 PHP에서 궁금
- 10. 중복 열 값에서 mySQL 키를 업데이트하는 방법
- 11. 내가 테이블에 매퍼가 나는 기업은 일부 propertie 여부가 있는지 여부를 참 또는 거짓을 선택해야 column_property을 정의 할 조건
- 12. bool 참 또는 거짓 및 음수 반환
- 13. PHP 암호, 비교, 참 또는 거짓
- 14. Prepared Statement에 참 또는 거짓 반환
- 15. 경우에 참/거짓 설정/또는 문
- 16. 참 또는 거짓 : forCellReuseIdentifier : 내보기 컨트롤러에서
- 17. 부울 출력이 참 또는 거짓 대신 205입니다.
- 18. simple_captcha_valid? 항상 거짓을 반환합니다
- 19. 거짓을 반환 한 후에 중단해야합니까?
- 20. 체크 박스는 항상 거짓을 반환합니다.
- 21. 거짓 Ajax.IsRequest를 반환하여 거짓을 반환합니다.
- 22. SmartGwt ListGrid.setAlwaysShowEditors (참) 문제
- 23. IF 문 항상 참
- 24. Java : Scanner 객체를 사용하여 온라인 상태에서 중복 토큰 수를 계산하십시오.
- 25. 중복 된 행을 가져 와서 단일 열을 기준으로 계산하십시오.
- 26. 함수 또는 변수가 호출되는 횟수를 계산하십시오.
- 27. 라우팅 신호의 전력 또는 수를 계산하십시오.
- 28. iReport에서 SHA1 또는 MD5 해시를 계산하십시오.
- 29. 단일 참 또는 거짓 값을 리턴하도록 ST_Overlap을 얻으려면 어떻게해야합니까?
- 30. bool 배열의 참 (또는 거짓) 요소 수를 계산 하시겠습니까?
우리에게 테이블 방법과 데이터가 테이블에 보일 것을 표시합니다. – BoltClock
이것은 PHP와 어떤 관련이 있습니까 ?? –
그렇다면 쿼리가 true를 12로, false를 3으로 반환하도록 하시겠습니까? 아니면 그것은 사용자에 의해 나뉘어지기를 원합니? 그래서 사용자 1에게는 3을, 사용자 9에 대해서는 9를 반환합니다. – Trott