简体   繁体   English

从多个条件为真的表中选择

[英]selecting from multiple tables where a condition is true

Trying to run some SQL with no luck, trying to select data from two tables where a condition is true. 尝试运行一些没有运气的SQL,尝试从两个满足条件的表中选择数据。

The tables are driver_details and locations. 这些表是driver_details和位置。 Both tables have the column user_id, and I want to get data from both based on the matched user_id from the between part. 这两个表都有一列user_id,我想根据两者之间匹配的user_id从两个表中获取数据。 (that select statement works and returns ID's); (该select语句起作用并返回ID);

SELECT driver_details.firstName,
                    locations.lat,
                    locations.lng
                    FROM driver_details
                    INNER JOIN locations
                    WHERE user_id = 
                    (SELECT user_id FROM locations WHERE 
                    (lat BETWEEN 0 AND 5) AND 
                    (lng BETWEEN 0 AND 5))

I am getting the error: Fatal error: Call to a member function bind_param() on a non-object in C:\\xampp\\htdocs\\vector\\www\\scripts\\getDriversInRange.php on line 33 我收到错误消息:致命错误:在第33行的C:\\ xampp \\ htdocs \\ vector \\ www \\ scripts \\ getDriversInRange.php中的非对象上调用成员函数bind_param()

You need to RELATE the 2 tables being JOINed using SOMETHING that's common in both tables, which is used to connect them ... you mention user_id 您需要使用两个表中常见的某种东西来关联要联接的两个表,这用于连接它们...您提到了user_id

...
FROM driver_details
INNER JOIN locations
ON driver_details.user_id = locations.user_id
WHERE ...
SELECT d.firstName,
    l.lat,
    l.lng
FROM driver_details d
    INNER JOIN locations l ON d.user_id = l.user_id
WHERE l.lat BETWEEN 0 AND 5
    AND l.lng BETWEEN 0 AND 5

This is basically a more complete example of what BWS posted. 这基本上是BWS发布内容的更完整示例。

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

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