简体   繁体   English

从两个不同的表但相同的数据库中选择 SQL 查询

[英]Select SQL query from two different tables but same database

i would like to select an sql query from two different tables.我想从两个不同的表中选择一个 sql 查询。 but both tables are in the same database.但是两个表都在同一个数据库中。

this is my sql code and "air, temp, humidity, mq2" are from one table known as the "pi_sensors_network" while "Dust" is from another table known as "pi_dust_sensor".这是我的 sql 代码,“空气、温度、湿度、mq2”来自一个名为“pi_sensors_network”的表,而“Dust”来自另一个名为“pi_dust_sensor”的表。 may i know how to go about selecting air, temp, humidity, mq2, Dust from 2 different tables but the same database?我可以知道如何从 2 个不同的表但同一个数据库中选择空气、温度、湿度、mq2、灰尘吗? Thanks!谢谢!

on a side note, there is no relation between both the tables.附带说明一下,两个表之间没有关系。 i just want to fetch the data from two different tables.我只想从两个不同的表中获取数据。

  $sql = "SELECT air, temp, humidity, mq2, Dust FROM pi_sensors_network ORDER BY time DESC LIMIT 1";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
     // output data of each row

Just use an alias for each table:只需为每个表使用别名:

SELECT t1.air, t1.temp, t1.humidity, t1.mq2, t1.Dust As Dust1, t2.Dust As Dust2
FROM pi_sensors_network t1, 
pi_dust_sensor t2
ORDER BY time DESC LIMIT 1";

You left a few things out in your schema description and query.您在架构描述和查询中遗漏了一些内容。 Do you have a field in one of the tables that refers to the equivalent record in the other table (ie a foreign key)?您在其中一个表中是否有一个字段引用另一个表中的等效记录(即外键)? If so, you can use a JOIN.如果是这样,您可以使用 JOIN。 You didn't mention which table has the time field.你没有提到哪个表有time字段。

Here is my stab at your desired query, but making assumptions on the unanswered questions.这是我对您想要的查询的尝试,但对未回答的问题进行了假设。 I assumed that you had a field pi_sensors_network.id that is a primary key for the pi_sensors_network table.我假设您有一个字段pi_sensors_network.idpi_sensors_network表的主键。 I also assumed that there was a field pi_dust_sensor.fkid that connected the rows in the pi_dust_sensor table with the rows in the pi_sensors_network table.我还假设有一个字段pi_dust_sensor.fkidpi_dust_sensor表中的行与pi_dust_sensor表中的行连接pi_sensors_network

SELECT air, temp, humidity, mq2, Dust
FROM pi_sensors_network INNER JOIN
    pi_dust_sensor ON pi_sensors_network.id = pi_dust_sensor.fkid
ORDER BY time DESC LIMIT 1

Hopefully that will point you in the right direction.希望这将为您指明正确的方向。 If you can answer my questions, I will be happy to update my answer.如果你能回答我的问题,我很乐意更新我的答案。

you can combine data of two table in one object and can use after that how you want the output look like:您可以将两个表的数据合并到一个对象中,然后可以使用您希望输出的样子:

SELECT * ( SELECT air, temp, humidity, mq2, '' As Dust,[time] FROM pi_sensors_network union all select '' as air, '' as temp, '' as humidity, '' as mq2, Dust As Dust,[time] from pi_dust_sensor )base ORDER BY [TIME] DESC SELECT * ( SELECT air, temp, height, mq2, '' As Dust,[time] FROM pi_sensors_network union all select '' as air, '' as temp, '' as湿度, '' as mq2, Dust As Dust,[ time] from pi_dust_sensor )base ORDER BY [TIME] DESC

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

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