Using multiple temporary tables in a single MySQL query (works in phpMyAdmin, but not PHP)

StackOverflow https://stackoverflow.com/questions/21907786

  •  14-10-2022
  •  | 
  •  

Domanda

I would like to avoid using real tables as a work-around. I am trying to join two tables and get unique values for the column I'm calling unique_values_needed, while also prioritizing the value 11 when it is present in duplicate real_secondary_table_primary_key.

Here is the code (it works in phpMyAdmin, but not in PHP):

CREATE TEMPORARY TABLE IF NOT EXISTS temporary_table_1
    AS (SELECT real_main_table.*, 
               real_secondary_table.real_secondary_table_primary_key 
          FROM real_main_table
               LEFT JOIN real_secondary_table 
                         ON real_secondary_table.real_main_table_primary_key = real_main_table.real_main_table_primary_key
      ORDER BY real_main_table.date DESC);

CREATE TEMPORARY TABLE IF NOT EXISTS temporary_table_2
    AS (SELECT real_main_table.*, 
               real_secondary_table.real_secondary_table_primary_key 
          FROM real_main_table
               LEFT JOIN real_secondary_table 
                         ON real_secondary_table.real_main_table_primary_key = real_main_table.real_main_table_primary_key
      ORDER BY real_main_table.date DESC);

UPDATE temporary_table_1 t1, temporary_table_2 t2
   SET t1.real_secondary_table_primary_key = t2.real_secondary_table_primary_key
 WHERE     t1.real_main_table_primary_key = t2.real_main_table_primary_key 
       AND t1.real_secondary_table_primary_key  11 
       AND t2.real_secondary_table_primary_key = 11;

  SELECT * 
    FROM temporary_table_1 
GROUP BY unique_values_needed
ORDER BY temporary_table_1.date DESC;

I have also tried the following (again, it works in phpMyAdmin, but still not in PHP):

CREATE TEMPORARY TABLE IF NOT EXISTS temporary_table_1
    AS (SELECT real_main_table.*, 
               real_secondary_table.real_secondary_table_primary_key 
          FROM real_main_table
               LEFT JOIN real_secondary_table 
                         ON real_secondary_table.real_main_table_primary_key = real_main_table.real_main_table_primary_key
      ORDER BY real_main_table.date DESC);

UPDATE temporary_table_1 t1, (
       SELECT real_main_table.*, 
              real_secondary_table.real_secondary_table_primary_key 
         FROM real_main_table
              LEFT JOIN real_secondary_table 
                        ON real_secondary_table.real_main_table_primary_key = real_main_table.real_main_table_primary_key
     ORDER BY real_main_table.date DESC) t2
   SET t1.real_secondary_table_primary_key = t2.real_secondary_table_primary_key
 WHERE     t1.real_main_table_primary_key = t2.real_main_table_primary_key 
       AND t1.real_secondary_table_primary_key  11 
       AND t2.real_secondary_table_primary_key = 11;

  SELECT * 
    FROM temporary_table_1 
GROUP BY unique_values_needed
ORDER BY temporary_table_1.date DESC;

When I checked using mysql_query($query) or die(mysql_error()), the error is supposedly either when creating the second temporary table (in the first code example), or in the UPDATE when using (SELECT ...) t2 (in the second code example).

È stato utile?

Soluzione

You cannot execute multiple queries with mysql_query() You should use mysqli_multi_query to do this. There is no equivalent of mysqli_multi_query in mysql_* functions.

You can use both mysql_* and mysqli_* in the same code but be careful not to mix them up like open with mysql_open() and query with mysqli_query() and vice versa. Once the connection is opened with mysqli_open() you should use only mysqli_* functions on that connection. This applies for mysql_* connection also. But you can have both connections open at the same time from the same code.

Warning: mysql_ function is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top