简体   繁体   English

从列具有特定文本的表中选择列的总和

[英]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 .第一家公司称为AG Trucks LTD ,另一家公司称为LC Trucks SERVICE STATION LTD The column of these is servicedby .这些列是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' sum(price) FROM Service Where 'servicedby' CONTAINS 'trucks'

Both columns price and servicedby are Varchar.列 price 和 servicedby 都是 Varchar。

There are a bunch of different ways to solve your problem.有很多不同的方法可以解决您的问题。

You can use the IN clause:您可以使用 IN 子句:

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

You can use the LIKE clause:您可以使用 LIKE 子句:

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

询问:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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