Select distinct values of distinct group in mysl, PHP
id title year ------------- 1 abc 1999 2 abc 2000 3 abc 2000 4 abc 1998 5
bce 1999 6 bce 1999 7 def 1999 8 def 1999 9 def 2000
I have a table similar to above
I am trying to get an array similar to
OUTPUT:
array('abc' => 'array(1999,2000,1998)', 'bce' => 'array(1999)', 'def' =>
'array(1999, 2000)');
I know a long way and I assume its time consuming way..
PHP:
$rid = mysql_query("SELECT DISTINCT title from table_name ORDER BY ASC);
while($arr = mysql_fetch_array($qr)){
$title = $arr
$rid = mysql_query("SELECT DISTINCT title from table_name ORDER BY ASC);
$yearArr = array();
while($arr2 = mysql_fetch_array($qr)){
$yearArr[] = $arr2['year'];
}
$finalArr = array(title => $yearArr);
}
finally i am getting the array I was looking for. but from the above code
if I have 1000 distinct titles then the whole process will executest 1001
queries.
Is there any short way of doing I also found one similar one but, I
couldnot go further
SELECT DISTINCT year,title FROM table_name WHERE title IN (SELECT DISTINCT
title FROM papers_scanned_01) ORDER BY title ASC, year DESC
using the above query i am getting an array as below
array( [0] => 'abc', [1] => '1999', [2] => '2000', [3] => '1998', [4] =>
'bce' and so on..
please help me. thanks in advance friends..
No comments:
Post a Comment