도저히 모르겠어서 질문 드립니다. SQL문...
본문
function update_category_rankings($url_path, $board_name) {
$sql = "SELECT wr_id, wr_subject, CAST(wr_10 AS UNSIGNED) AS wr_10,
COALESCE(wr_good, 0) AS wr_good
FROM g5_write_{$url_path}
WHERE wr_10 IS NOT NULL AND wr_nogood < 1
ORDER BY (wr_10 * 1.5 + wr_good * 1.3) DESC
LIMIT 100";
$result = sql_query($sql);
$rank = 1;
while ($row = sql_fetch_array($result)) {
$score = ($row['wr_10'] * 0.7) + ($row['wr_good'] * 1.3);
$prev_category_rank_sql = "SELECT category_rank FROM g5_rank_list WHERE wr_id = '{$row['wr_id']}' AND url_path = '{$url_path}' ORDER BY id DESC LIMIT 1";
$prev_category_rank_result = sql_query($prev_category_rank_sql);
$prev_category_rank_row = sql_fetch_array($prev_category_rank_result);
$previous_category_rank = $prev_category_rank_row ? $prev_category_rank_row['category_rank'] : null;
$upsert_sql = "INSERT INTO g5_rank_list
(wr_id, category_rank, previous_category_rank, subject, score, good, board_name, url_path)
VALUES (
'{$row['wr_id']}',
{$rank},
" . ($previous_category_rank !== null ? $previous_category_rank : "NULL") . ",
'" . sql_real_escape_string($row['wr_subject']) . "',
{$score},
{$row['wr_good']},
'{$board_name}',
'{$url_path}'
)
ON DUPLICATE KEY UPDATE
category_rank = VALUES(category_rank),
previous_category_rank = VALUES(previous_category_rank),
subject = VALUES(subject),
score = VALUES(score),
good = VALUES(good)";
sql_query($upsert_sql);
$rank++;
}
}
function update_overall_rankings() {
$sql = "SELECT wr_id, wr_subject, CAST(wr_10 AS UNSIGNED) AS wr_10,
COALESCE(wr_good, 0) AS wr_good
FROM g5_write_all
WHERE wr_10 IS NOT NULL AND wr_nogood < 1
ORDER BY (wr_10 * 1.5 + wr_good * 1.3) DESC
LIMIT 100";
$result = sql_query($sql);
$rank = 1;
while ($row = sql_fetch_array($result)) {
$score = ($row['wr_10'] * 0.7) + ($row['wr_good'] * 1.3);
$prev_overall_rank_sql = "SELECT overall_rank FROM g5_rank_list WHERE wr_id = '{$row['wr_id']}' ORDER BY id DESC LIMIT 1";
$prev_overall_rank_result = sql_query($prev_overall_rank_sql);
$prev_overall_rank_row = sql_fetch_array($prev_overall_rank_result);
$previous_overall_rank = $prev_overall_rank_row ? $prev_overall_rank_row['overall_rank'] : null;
$upsert_sql = "INSERT INTO g5_rank_list
(wr_id, overall_rank, previous_overall_rank, subject, score, good, board_name, url_path)
VALUES (
'{$row['wr_id']}',
{$rank},
" . ($previous_overall_rank !== null ? $previous_overall_rank : "NULL") . ",
'" . sql_real_escape_string($row['wr_subject']) . "',
{$score},
{$row['wr_good']},
'ALL',
'all'
)
ON DUPLICATE KEY UPDATE
overall_rank = VALUES(overall_rank),
previous_overall_rank = VALUES(previous_overall_rank),
subject = VALUES(subject),
score = VALUES(score),
good = VALUES(good),
board_name = 'ALL',
url_path = 'all'";
sql_query($upsert_sql);
$rank++;
}
}
소스는 대략 이렇습니다. 전체 카운터와 카테고리 카운터를 등록을 하려고 하는데 카운터가 카테고리는 등록이 되는데 전체 필드가 등록이 안되는 현상입니다.
overall_rank, previous_overall_rank 이 두개의 필드가 죽어도 안들어가네요...데이터가 숫자가 들어가야 하는데 DB를 봐도 안들어가 있고 희안한건 category_rank, previous_category_rank 이것 두개는 데이터가 들어갑니다...ㅠㅠ 고수님들 도와주세요..
답변 3
쿼리를 직접 찍어서 확인해 보시고 phpMyAdmin에서 직접 쿼리를 실행해서 문제를 체크해 보셔야 할듯 합니다.
플라이님 말씀처럼 phpmyadmin 에서 SQL 쿼리해보세요.
왠만한 오류들은 알려줍니다.
{$rank}, -> '{$rank}', 이런식으로 변경해보세요.
추측하기로 특정 값이 없어서 에러나는것 같네요.