 |
วิธีตัดค่าซ้ำกันใน sql มันตัดออกยังไงครับ รายละเอียดข้างใน รบกวนทีครับ |
|
 |
|
|
 |
 |
|
Code (PHP)
$sql = "select distinct id from tb order by id";
$result = mysql_query($sql) or die(mysql_error());
while($data = mysql_fetch_row($result)){
echo "{$data[0]} - ";
$sql="select data from tb where id='{$data[0]}' ";
$result2 = mysql_query($sql) or die(mysql_error());
while($data2 = mysql_fetch_row($result2)){
echo "{$data2[0]}, ";
}
echo "<br />\n";
}
|
 |
 |
 |
 |
Date :
2013-03-21 15:53:27 |
By :
GS-Battery |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ต้อง select สองครั้งเหรอครับ
|
 |
 |
 |
 |
Date :
2013-03-21 17:36:26 |
By :
novicecode |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ใช้ SQL ประมาณนี้ครับ
ได้อย่างที่ต้องการในครั้งเดียว
SELECT
CONCAT(`id`, ' ', GROUP_CONCAT(DISTINCT `data` ORDER BY `data` SEPARATOR ' , '))
FROM `table`
GROUP BY `id`
ORDER BY `id`
|
 |
 |
 |
 |
Date :
2013-03-21 19:47:02 |
By :
cookiephp |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (PHP)
$result = mysql_query("
SELECT
CONCAT(`id`, ' ', GROUP_CONCAT(DISTINCT `data` ORDER BY `data` SEPARATOR ' , ')) AS `output`
FROM `ชื่อตารางของคุณ`
GROUP BY `id`
ORDER BY `id`
");
while (($row = mysql_fetch_assoc($result))) {
echo "$row[output]<br />";
}
|
 |
 |
 |
 |
Date :
2013-03-21 19:49:43 |
By :
cookiephp |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ไม่รู้ว่ามาตอบช้าไปหรือเปล่าครับ
$sql = "SELECT id, concat_ws( ',', group_concat( distinct data order by data ) ) FROM `b` group by id ";
$result = mysql_query($sql) or die(mysql_error());
while($data = mysql_fetch_row($result)){
echo "{$data[0]} - {$data[1]} <br />\n";
}
|
 |
 |
 |
 |
Date :
2013-03-22 00:01:49 |
By :
sakuraei |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เด่วผมลองไปทำแปปนึงครับ
|
 |
 |
 |
 |
Date :
2013-03-22 01:23:37 |
By :
novicecode |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ได้แล้วครับ ขอบคุณทุกท่านที่ตอบนะครับ ^^

ผมใช้ code ไปรันใน sql appserve ครับ
Code (PHP)
SELECT `group_id`, concat_ws( ',', group_concat( distinct `DurableArticleSet`order by `DurableArticleSet` ) ) FROM `reserve` group by `group_id`
|
 |
 |
 |
 |
Date :
2013-03-22 01:40:53 |
By :
novicecode |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|

|
 |
 |
 |
 |
Date :
2013-03-22 06:18:59 |
By :
mr.win |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|