0

I need to use select id inside another select but I don't know how can do it? My idea is something like this:

$query10 = "SELECT id FROM $database_table WHERE UTCdate >= '{$from_date}' AND satellite_derived_time >= CAST('{$from_time}' AS UNSIGNED)";

$query20 = "SELECT id FROM $database_table WHERE UTCdate >= '{$to_date}' AND satellite_derived_time >= CAST('{$to_time}' AS UNSIGNED)";


$query = "SELECT * FROM $database_table WHERE id BETWEEN '$query10' AND '$query20'";

1 Answer 1

1

Use JOINs

SELECT t.*
FROM $database_table AS tfrom
JOIN $database_table AS tto
JOIN $database_table AS t ON t.id BETWEEN tfrom.id AND tto.id
WHERE tfrom.UTCdate >= '$from_date' AND tfrom.satellite_derived_time >= CAST('$from_time' AS UNSIGNED)
AND tto.UTCdate >= '$to_date' AND tto.satellite_derived_time >= CAST('$to_time' AS unsigned)

or use subqueries:

SELECT *
FROM $database_table
WHERE id BETWEEN (SELECT id FROM $database_table WHERE UTCdate >= '{$from_date}' AND satellite_derived_time >= CAST('{$from_time}' AS UNSIGNED))
             AND (SELECT id FROM $database_table WHERE UTCdate >= '{$to_date}' AND satellite_derived_time >= CAST('{$to_time}' AS UNSIGNED))
Sign up to request clarification or add additional context in comments.

7 Comments

Query failedsubquery returns more than one row.
I think there is a problem and it shows me above message. I used second method (subqueries).
Your original code implies that there can only be one value in $query10 and $query20. How are multiple IDs supposed to be handled? Your original code doesn't make sense if there can be multiple IDs.
Yes, there can be just one value in $query10 and $query20
The error message says that they're returning more than one value.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.