tags:

views:

77

answers:

1

Hello.. I have a database table as follows:

line     model        serial     range serial    
fa01     kd-g335ud   105x0001    105x0001-105x0200
fa01     kd-g335ud   105x0002    105x0001-105x0200 
fa01     kd-g335ud   105x0003    105x0001-105x0200 
fa02     kd-r311ed   105x0001    105x0001-105x0500
fa02     kd-r311ed   105x0002    105x0001-105x0500

When I query it, I want the result to be like this:

line     model       qty        qty range
fa01     kd-g335ud   3            200
fa02     kd-r311ed   2            500

I have written the following query, however I am still still confused as to how to show qty range.

 //this just show until qty
    SELECT line, model, COUNT(serial) as qty FROM inspection GROUP BY line, model

    //this for search string
    $sql_result=array(//what must i fill?);
    foreach($sql_result as $row)
    {
       preg_match_all('/\d{4}/',$row,$matches);
       echo intval($matches[0][1])-intval($matches[0][0])+1;
    }

Please help, as I am still unsure as to the second syntax

+1  A: 

Assuming that qty range is the last three characters of range_serial

SELECT line, 
       model, 
       COUNT(serial) as qty,
       RIGHT(range_serial,3) as qty_range 
  FROM inspection 
 GROUP BY line, 
          model,
          range_serial

EDIT

$sqlQuery = 'SELECT line, model, COUNT(serial) as qty,range_serial as qty_range 
               FROM inspection 
              GROUP BY line, model, range_serial';

$sqlResult = mysql_query($sqlQuery); 

while ($row = mysql_fetch_assoc($sqlResult)) {
   preg_match_all('/\d{4}/',$row['qty_range'],$matches); 
   echo intval($matches[0][1])-intval($matches[0][0])+1; 
} 
Mark Baker
no..the qty range get from search n counting like my second code.my problem is to combine both syntax.
klox
What I can't work out is what you're searching and counting?
Mark Baker
$row is an array, comprising the three columns from the selected row, so preg_match_all won't work. What are you trying to extract, and from what
Mark Baker
i want script can do like : 105x0001-105x0500 result 500.it means use regex for search 0001 and 0500.then do counting 0500-0100= [use intval($matches[0][1])-intval($matches[0][0])+1],and show 500,after that this script will work together with mysql.
klox
To start then, you need to retrieve range_serial from the database in your SQL query. Then you need to do your preg_match_all against $row['range_serial'], not against $row
Mark Baker
can you edit your answer so i can more understand
klox