SQL을 만들다가 IQ 부족으로 도움 청합니다. 정보
SQL을 만들다가 IQ 부족으로 도움 청합니다.본문
네이버처럼... 내가 쓴 글에 최근 3일 안에 코멘트가 붙으면 보여주려고 하는데...
sql을 만들다가 IQ 부족으로 ... ㅠ..ㅠ
내가 쓴 글 .... select * from g4_board_new where wr_id = wr_parent and mb_id = 'echo4me'
코멘트들 ... select * from g4_board_new where wr_id != wr_parent
코멘트의 wr_parent가 wr_id이고 bo_table이 같은 record 중... mb_id가 내꺼인거를 찾는거에요.
SQL 한번에 가능할 것 같은데 막상 해보려니까 어렵네요. ㅠ..ㅠ
select * from g4_board_new
where wr_id in (select wr_parent from g4_board_new where wr_id != wr_parent)
and mb_id = 'echo4me'
and wr_id = wr_parent
order by bn_id desc
이것이 맞는 SQL 문장인지, 더 간략하게는 안되는지 확인 부탁드립니다.
댓글 전체
댓글을 보여주는건가요, 아니면 원본글을 보여주는건가요??
원본글이요^^
느므 어려운걸..ㅠㅠ
일단 불당님이 만드신 쿼리는 결과적으로 echo4me 가 작성한 글만 전부 가져오게됩니다.
"wr_id in (select wr_parent from g4_board_new where wr_id != wr_parent) "
in 안에 wr_id 값이 없고 wr_parent 값만 있기 때문에 이 부분 쿼리는 결과값이 없게 됩니다.
where mb_id = 'echo4me' and wr_id = wr_parent 쿼리랑 같은 결과가 나오게 되는거죠..
그렇다고 "wr_id in (select wr_id from g4_board_new where wr_id != wr_parent) " 로 처리하면
뒤에 있는 and wr_id = wr_parent 부분 때문에 논리적으로 오류가 발생합니다.
이 역시 결과값은 나오지 않습니다.
아래 제가 만들어본 쿼리는 테스트는 해봤습니다만 정확한지는 확신이 서지 않습니다.
테스트 해보시고 사용하시길 바랍니다.
select
n1.bo_table, n1.wr_parent
from
g4_board_new as n1,
(select wr_id, bn_datetime
from g4_board_new
where wr_id = wr_parent
and mb_id='echo4me'
) as n2
where
n1.wr_parent <> n1.wr_id
and n1.wr_parent = n2.wr_id
and datediff(curdate(), n2.bn_datetime) <= 3
group by n1.bo_table, n1.wr_parent
order by n1.wr_parent desc
게시판에서 값을 가져올때는 wr_id = $row[wr_parent] 와 같은 형식으로 가져오셔야 합니다.
일단 불당님이 만드신 쿼리는 결과적으로 echo4me 가 작성한 글만 전부 가져오게됩니다.
"wr_id in (select wr_parent from g4_board_new where wr_id != wr_parent) "
in 안에 wr_id 값이 없고 wr_parent 값만 있기 때문에 이 부분 쿼리는 결과값이 없게 됩니다.
where mb_id = 'echo4me' and wr_id = wr_parent 쿼리랑 같은 결과가 나오게 되는거죠..
그렇다고 "wr_id in (select wr_id from g4_board_new where wr_id != wr_parent) " 로 처리하면
뒤에 있는 and wr_id = wr_parent 부분 때문에 논리적으로 오류가 발생합니다.
이 역시 결과값은 나오지 않습니다.
아래 제가 만들어본 쿼리는 테스트는 해봤습니다만 정확한지는 확신이 서지 않습니다.
테스트 해보시고 사용하시길 바랍니다.
select
n1.bo_table, n1.wr_parent
from
g4_board_new as n1,
(select wr_id, bn_datetime
from g4_board_new
where wr_id = wr_parent
and mb_id='echo4me'
) as n2
where
n1.wr_parent <> n1.wr_id
and n1.wr_parent = n2.wr_id
and datediff(curdate(), n2.bn_datetime) <= 3
group by n1.bo_table, n1.wr_parent
order by n1.wr_parent desc
게시판에서 값을 가져올때는 wr_id = $row[wr_parent] 와 같은 형식으로 가져오셔야 합니다.
답을 올려주시기 전에 이렇게 했는데... 코멘트가 없는 내글도 나오네요. ..ㅠ..ㅠ
<?
//if (!defined("_GNUBOARD_")) exit; // 개별 페이지 접근 불가
include_once("./_common.php");
$g4[title] = "my page";
include_once("$g4[path]/_head.php");
// 최근 $my_date일 사이에 코멘트가 있는 게시글만 검색한다.
if (! $my_date)
$my_date = 7; // 최근
$sql = " select * from $g4[board_new_table]
where wr_id in (select wr_parent from g4_board_new where wr_id != wr_parent)
and mb_id = '$member[mb_id]'
and wr_id = wr_parent
and bn_datetime >= adddate(curdate(), interval -$my_date day)
order by bn_id desc ";
$result = mysql_query($sql);
$total_count = mysql_num_rows($result);
?>
<table width=100% cellpadding=0 cellspacing=0>
<tr><td>최근 <?=$my_date?> 일 동안 코멘트가 있는 나의 게시글은 <b><?=$total_count?></b> 건 입니다.
</td></tr>
<tr><td height="10"></td></tr>
<tr><td>
<?//출력
for ($i=0; $row = sql_fetch_array($result); $i++){
?>
<?
$sql2 = " select wr_subject from $g4[write_prefix]$row[bo_table] where wr_id = '$row[wr_id]' ";
$row2 = sql_fetch($sql2);
$sql3 = " select max(wr_id) as max_cn from $g4[write_prefix]$row[bo_table] where wr_parent = '$row[wr_id]' ";
$row3 = sql_fetch($sql3);
?>
<a href='<?=$g4[bbs_path]?>/board.php?bo_table=<?=$row[bo_table]?>&wr_id=<?=$row[wr_id]?>#c_<?=$row3[max_cn]?>' onfocus='this.blur()'>
<?=cut_str($row2[wr_subject], 60)?><br>
</a>
<? } ?>
</td></tr>
</table>
<?
include_once("$g4[path]/_tail.php");
?>
<?
//if (!defined("_GNUBOARD_")) exit; // 개별 페이지 접근 불가
include_once("./_common.php");
$g4[title] = "my page";
include_once("$g4[path]/_head.php");
// 최근 $my_date일 사이에 코멘트가 있는 게시글만 검색한다.
if (! $my_date)
$my_date = 7; // 최근
$sql = " select * from $g4[board_new_table]
where wr_id in (select wr_parent from g4_board_new where wr_id != wr_parent)
and mb_id = '$member[mb_id]'
and wr_id = wr_parent
and bn_datetime >= adddate(curdate(), interval -$my_date day)
order by bn_id desc ";
$result = mysql_query($sql);
$total_count = mysql_num_rows($result);
?>
<table width=100% cellpadding=0 cellspacing=0>
<tr><td>최근 <?=$my_date?> 일 동안 코멘트가 있는 나의 게시글은 <b><?=$total_count?></b> 건 입니다.
</td></tr>
<tr><td height="10"></td></tr>
<tr><td>
<?//출력
for ($i=0; $row = sql_fetch_array($result); $i++){
?>
<?
$sql2 = " select wr_subject from $g4[write_prefix]$row[bo_table] where wr_id = '$row[wr_id]' ";
$row2 = sql_fetch($sql2);
$sql3 = " select max(wr_id) as max_cn from $g4[write_prefix]$row[bo_table] where wr_parent = '$row[wr_id]' ";
$row3 = sql_fetch($sql3);
?>
<a href='<?=$g4[bbs_path]?>/board.php?bo_table=<?=$row[bo_table]?>&wr_id=<?=$row[wr_id]?>#c_<?=$row3[max_cn]?>' onfocus='this.blur()'>
<?=cut_str($row2[wr_subject], 60)?><br>
</a>
<? } ?>
</td></tr>
</table>
<?
include_once("$g4[path]/_tail.php");
?>
제가 드린 쿼리로 해보시라니깐요.ㅎㅎ
ㅎㅎ... 제 쿼리는 문제 확인했고 알려주신 것으로 해보니까...
다른 사람들 글에 제 덧글이 있는 것도 불러와요.
그것은 불러오면 안되는거쟎아요. ㅠ..ㅠ
아래처럼 하니까 오류 나오네요. 흙~ 어려워... ㅠ..ㅠ
from
g4_board_new as n1,
left join
(select wr_id, bn_datetime
from g4_board_new
where wr_id = wr_parent
and mb_id='$member[mb_id]'
) as n2
on (n1.bn_id = n2.bn_id)
다른 사람들 글에 제 덧글이 있는 것도 불러와요.
그것은 불러오면 안되는거쟎아요. ㅠ..ㅠ
아래처럼 하니까 오류 나오네요. 흙~ 어려워... ㅠ..ㅠ
from
g4_board_new as n1,
left join
(select wr_id, bn_datetime
from g4_board_new
where wr_id = wr_parent
and mb_id='$member[mb_id]'
) as n2
on (n1.bn_id = n2.bn_id)
소스좀 보여주시겠어요?
<?
//if (!defined("_GNUBOARD_")) exit; // 개별 페이지 접근 불가
include_once("./_common.php");
$g4[title] = "my page";
include_once("$g4[path]/_head.php");
// 최근 $my_date일 사이에 코멘트가 있는 게시글만 검색한다.
if (! $my_date)
$my_date = 3; // 최근
$sql = " select n1.*
from
g4_board_new as n1,
(select *
from g4_board_new
where wr_id = wr_parent
and mb_id='$member[mb_id]'
) as n2
where
n1.wr_parent <> n1.wr_id
and n1.wr_parent = n2.wr_id
and datediff(curdate(), n2.bn_datetime) <= $my_date
group by n1.bo_table, n1.wr_parent
order by n1.bn_datetime desc ";
$result = mysql_query($sql);
$total_count = mysql_num_rows($result);
?>
<table width=100% cellpadding=0 cellspacing=0>
<tr><td>최근 <?=$my_date?> 일 동안 코멘트가 있는 나의 게시글은 <b><?=$total_count?></b> 건 입니다.
</td></tr>
<tr><td height="10"></td></tr>
<tr><td>
<?//출력
for ($i=0; $row = sql_fetch_array($result); $i++){
?>
<?
$sql2 = " select wr_subject from $g4[write_prefix]$row[bo_table] where wr_id = '$row[wr_id]' ";
$row2 = sql_fetch($sql2);
echo $row2[wr_subject];
$sql3 = " select max(wr_id) as max_cn from $g4[write_prefix]$row[bo_table] where wr_parent = '$row[wr_id]' ";
$row3 = sql_fetch($sql3);
?>
<?=$i+1?>
<a href='<?=$g4[bbs_path]?>/board.php?bo_table=<?=$row[bo_table]?>&wr_id=<?=$row[wr_id]?>#c_<?=$row3[max_cn]?>' onfocus='this.blur()'>
<?=$row[bo_table]?><?=cut_str($row2[wr_subject], 60)?><br>
</a>
<? } ?>
</td></tr>
</table>
<?
include_once("$g4[path]/_tail.php");
?>
//if (!defined("_GNUBOARD_")) exit; // 개별 페이지 접근 불가
include_once("./_common.php");
$g4[title] = "my page";
include_once("$g4[path]/_head.php");
// 최근 $my_date일 사이에 코멘트가 있는 게시글만 검색한다.
if (! $my_date)
$my_date = 3; // 최근
$sql = " select n1.*
from
g4_board_new as n1,
(select *
from g4_board_new
where wr_id = wr_parent
and mb_id='$member[mb_id]'
) as n2
where
n1.wr_parent <> n1.wr_id
and n1.wr_parent = n2.wr_id
and datediff(curdate(), n2.bn_datetime) <= $my_date
group by n1.bo_table, n1.wr_parent
order by n1.bn_datetime desc ";
$result = mysql_query($sql);
$total_count = mysql_num_rows($result);
?>
<table width=100% cellpadding=0 cellspacing=0>
<tr><td>최근 <?=$my_date?> 일 동안 코멘트가 있는 나의 게시글은 <b><?=$total_count?></b> 건 입니다.
</td></tr>
<tr><td height="10"></td></tr>
<tr><td>
<?//출력
for ($i=0; $row = sql_fetch_array($result); $i++){
?>
<?
$sql2 = " select wr_subject from $g4[write_prefix]$row[bo_table] where wr_id = '$row[wr_id]' ";
$row2 = sql_fetch($sql2);
echo $row2[wr_subject];
$sql3 = " select max(wr_id) as max_cn from $g4[write_prefix]$row[bo_table] where wr_parent = '$row[wr_id]' ";
$row3 = sql_fetch($sql3);
?>
<?=$i+1?>
<a href='<?=$g4[bbs_path]?>/board.php?bo_table=<?=$row[bo_table]?>&wr_id=<?=$row[wr_id]?>#c_<?=$row3[max_cn]?>' onfocus='this.blur()'>
<?=$row[bo_table]?><?=cut_str($row2[wr_subject], 60)?><br>
</a>
<? } ?>
</td></tr>
</table>
<?
include_once("$g4[path]/_tail.php");
?>
$row[wr_parent] 를 사용하시라니까요.. ㅠㅠ
아래 코드로 해보세요.
<?//출력
for ($i=0; $row = sql_fetch_array($result); $i++){
?>
<?
$sql2 = " select wr_subject from $g4[write_prefix]$row[bo_table] where wr_id = '$row[wr_parent]' ";
$row2 = sql_fetch($sql2);
echo $row2[wr_subject];
$sql3 = " select max(wr_id) as max_cn from $g4[write_prefix]$row[bo_table] where wr_parent = '$row[wr_parent]' ";
$row3 = sql_fetch($sql3);
?>
<?=$i+1?>
<a href='<?=$g4[bbs_path]?>/board.php?bo_table=<?=$row[bo_table]?>&wr_id=<?=$row[wr_id]?>#c_<?=$row3[max_cn]?>' onfocus='this.blur()'>
<?=$row[bo_table]?><?=cut_str($row2[wr_subject], 60)?><br>
</a>
<? } ?>
아래 코드로 해보세요.
<?//출력
for ($i=0; $row = sql_fetch_array($result); $i++){
?>
<?
$sql2 = " select wr_subject from $g4[write_prefix]$row[bo_table] where wr_id = '$row[wr_parent]' ";
$row2 = sql_fetch($sql2);
echo $row2[wr_subject];
$sql3 = " select max(wr_id) as max_cn from $g4[write_prefix]$row[bo_table] where wr_parent = '$row[wr_parent]' ";
$row3 = sql_fetch($sql3);
?>
<?=$i+1?>
<a href='<?=$g4[bbs_path]?>/board.php?bo_table=<?=$row[bo_table]?>&wr_id=<?=$row[wr_id]?>#c_<?=$row3[max_cn]?>' onfocus='this.blur()'>
<?=$row[bo_table]?><?=cut_str($row2[wr_subject], 60)?><br>
</a>
<? } ?>
다 수정했습니다. 감사합니다. 스킨에 올릴께요 ^^