简体   繁体   English

sqlite3查询多个条件的PHP

[英]sqlite3 query multiple conditions php

I have a database that looks like this: 我有一个看起来像这样的数据库:

Owner   Vehicle   Color

Peter   Car       Black
Peter   Bike      Black
Peter   Bike      Red
Peter   Bike      Black
Marc    Car       Black
Marc    Car       Black
Marc    Bike      Red
Marc    Bike      Red

What I need is a query that goes through the whole database and eventually provide me an overview, such as: 我需要的是遍历整个数据库并最终向我提供概述的查询,例如:

  • Peter has 1 black car 彼得有1辆黑色轿车
  • Peter has 2 black bikes 彼得有2辆黑色自行车
  • Peter has 1 red bike 彼得有一辆红色自行车
  • Marc has 2 black cars 马克有2辆黑色轿车
  • Marc has 2 red bikes 马克有2辆红色自行车

So for each owner I would want to have the count (and name) of the instances of vehicle+color. 因此,对于每个车主,我希望获得车辆+颜色实例的数量(和名称)。

What is the best way with PHP to approach this for a database with 900,000 rows. 对于具有900,000行的数据库,PHP最好的方法是什么。 I have a list with the names of the owners, but how do I count the instances of vehicle+color in an effective way? 我有一个列出车主姓名的清单,但是如何有效计算出车辆+颜色的实例呢?

You'll need to use COUNT with a GROUP BY to aggregate per row. 您需要将COUNTGROUP BY以汇总每行。 Since you want to know how many of each vehicle and color an owner has, you need to group by all three columns: 由于您想知道车主拥有的每种车辆和颜色的数量,因此需要按以下三列进行分组:

SELECT owner, vehicle, color, COUNT(*) as vehicle_count
FROM mytable
GROUP BY owner, vehicle, color

Result: 结果:

+-------+---------+-------+---------------+
| owner | vehicle | color | vehicle_count |
+-------+---------+-------+---------------+
| Marc  | Bike    | Red   |             2 |
| Marc  | Car     | Black |             2 |
| Peter | Bike    | Black |             2 |
| Peter | Bike    | Red   |             1 |
| Peter | Car     | Black |             1 |
+-------+---------+-------+---------------+

After executing the query, these rows can then be read out by your PHP code like 执行查询后,这些行可以由您的PHP代码读取,例如

echo $row["owner"]." has ".$["vehicle_count"]." ".$vehicles;

or processed however you want to present the information. 或已处理,但是您想显示信息。

you can use the following mysqli code.. don't forget to change the "username", "password", "database "in connection and tablename in $get_owner and $get_num. 您可以使用下面的mysqli代码。.不要忘记在$ get_owner和$ get_num中更改连接和表名中的“用户名”,“密码”,“数据库”。

search for the owner and the result will be shown. 搜索所有者,将显示结果。

code:- 码:-

<form action="" method="post">
  <input type="text" placeholder="Owner name" name="owner" />
  <input type="submit" value="search" name="search"/>

</form>

<?php

$conn = mysqli_connect ("localhost", "username", "password", "database");

if (mysqli_connect_errno()) {
  echo "Error ".mysqli_connect_errno();
}

if(isset($_POST['search'])) {
  $owner = $_POST['owner'];
    $get_owner = "SELECT * FROM `tablename` WHERE Owner='$owner'";
  $run = mysqli_query($conn, $get_owner);

while ($row =  mysqli_fetch_array($run)) {

    $vehical = $row['Vehicle'];
    $color = $row['Color'];

    $get_num = "SELECT `tablename` FROM `efgh` WHERE Vehicle='$vehical' and Color='$color'";
    $run_num = mysqli_query($conn ,$get_num);
    $chekc = mysqli_num_rows($run_num);

    echo $owner.' has '.$chekc.' '.$color.' '.$vehical.'<br>';
  }
}

 ?>

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

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