Sample Header Ad - 728x90

Table Aliases: MySQL says table R1 does not exist when Joining the same table 3 times

1 vote
2 answers
646 views
I have a table R with the following fields stated below: weatherID, dateRecorded, Days, locationID, timeRecorded, rainAmount, rainNumberHours, temperature, windSpeed, windDirection NB: The Days field is an integer type that describes the number of days with auto-increment +1. The weatherID field is a concatenation of two other fields in two different tables not shown here. This schema of the table is simplified, but it is relevant to what I want to investigate. We want to find out if there exist some weather patterns that follow other weather patterns and to find records that satisfy specific parameters and conditions. I use aliases in the query to get records for different field values Days. A model query will make it clear: I am joining the table R, multiple times in an alias style query so that I have alias R1 and alias R2 making it look like joining three tables together.
$sql = "
SELECT R.weatherID, R.Days, R.locationID, R.timeRecorded, R.windDirection, 
R1.weatherID, R1.Days, R1.locationID, R1.timeRecorded, R1.windDirection, 
R2.weatherID, R2.Days, R2.locationID, R2.timeRecorded, R2.windDirection 
FROM ((R 
INNER JOIN R1 ON R.Days= R1.Days) 
INNER JOIN R2 ON R.locationID= R2.locationID) 
WHERE ((R.Days)=[R1].[Days]-2) AND ((R1.Days)=[R2].[Days]-2);
"
This model gives me similar weather patterns that re-occurs or repeat within two (2) days (refers to the "2" in the WHERE clause). Now the question is how do we I want to arrange the results of the query in a grid-like manner in the following way: R.weatherID R.Days R.locationID R.windDirection R1.weatherID R1.Days R1.locationID R1.windDirection R2.weatherID R2.Days R2.locationID R2.windDirection And the next record is also in the same grid-like format. I have only table R in the database: that is I have weatherdb.R, but I don't have "weatherdb.R1" and "weatherdb.R2" in other words, I don't have tables R1 and R2 but am using them in aliases, which is not working. But, MySQL said: Documentation #1146 - Table 'weatherdb.R1' doesn't exist The sqlWeather1.php file is my first trial example below:
");
$first_row = true;
while ($row = mysqli_fetch_assoc($result)) {
    if ($first_row) {
        $first_row = false;
        // Output header row from keys.
        echo '';
        foreach($row as $key => $field) {
            echo '' . htmlspecialchars($key) . '';
        }
        echo '';
    }
    echo '';
    foreach($row as $key => $field) {
        echo '' . htmlspecialchars($field) . '';
    }
    echo '';
}
echo("");
?>
Asked by BenGik (13 rep)
Oct 7, 2022, 09:28 PM
Last activity: Oct 8, 2022, 07:51 AM