简体   繁体   中英

Select sum of column from a table where column has specific text

I have this table where we add all the services for the companies vehicles.

I want to select the sum from the column price from which the vehicles where serviced by this two specific companies.

The first company is called AG Trucks LTD and other one LC Trucks SERVICE STATION LTD . The column of these is servicedby .

Currently I use this of which has no results:

case 'getstats':

            $q=mysql_real_escape_string($_GET['q']);
            $query="SELECT sum(price) FROM `Service` Where `servicedby` = 'Trucks' ";

            $result = mysql_query($query);


            $temp=0;
            $json = array();
                while ($row = mysql_fetch_array($result)) {
                    $json[$temp]['servicedby'] = $row['servicedby'];
                    $json[$temp]['price'] = $row['price'];
            $temp++;
                }

            print json_encode($json);




            mysql_close();


        break;

So how can I select:

sum(price) FROM Service Where 'servicedby' CONTAINS 'trucks'

Both columns price and servicedby are Varchar.

There are a bunch of different ways to solve your problem.

You can use the IN clause:

SELECT sum(price) FROM `Service` Where `servicedby` IN ('A.G Trucks LTD','LC Trucks SERVICE STATION LTD')

You can use the LIKE clause:

SELECT sum(price) FROM `Service` Where `servicedby` LIKE '%Trucks%'

询问:

SELECT sum(price) FROM `Service` Where `servicedby` like '%Trucks%'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM