Home >Backend Development >PHP Tutorial >mysql_field_table is invalid after union?

mysql_field_table is invalid after union?

WBOY
WBOYOriginal
2016-08-04 09:22:031105browse

————————
mysql_field_table is a function to get the data table name.
Under normal circumstances, sql select only queries 1 table, and it can be executed.
Excuse me: when sql select queries multiple tables in union , how to get multiple table names?
——————
Executable code:
//sql select only queries 1 piece of data

<code>$sql = "
    SELECT id,title FROM `TABLE1` WHERE id IN($id)
    ORDER BY id DESC
    LIMIT 0,10
";
$result = mysql_query($sql);
$table_name = mysql_field_table($result,0);
echo "<br />表名:$table_name";</code>

——————
Question code:

<code>//合并2个表,按时间倒序取出前10条记录
$sql = "
    SELECT id,title FROM `TABLE1` WHERE id IN($id)
    UNION
    SELECT id,title FROM `TABLE2` WHERE id IN($id)
    UNION
    ORDER BY id DESC
    LIMIT 0,10
";
$result = mysql_query($sql);

if( $result ){
    if( mysql_num_rows($result) ){
        $a = array();
        while($arr = mysql_fetch_array($result)){
            $table_name = mysql_field_table($result,0);
            echo "<br />表名:$table_name";
            //***就是在这里
            //***请问各位,怎么获取多个表名
        }
    }
}</code>

——————
Salute,
Thank you!

Reply content:

————————
mysql_field_table is a function to get the data table name.
Under normal circumstances, sql select only queries 1 table, and it can be executed.
Excuse me: when sql select queries multiple tables in union , how to get multiple table names?
——————
Executable code:
//sql select only queries 1 piece of data

<code>$sql = "
    SELECT id,title FROM `TABLE1` WHERE id IN($id)
    ORDER BY id DESC
    LIMIT 0,10
";
$result = mysql_query($sql);
$table_name = mysql_field_table($result,0);
echo "<br />表名:$table_name";</code>

——————
Question code:

<code>//合并2个表,按时间倒序取出前10条记录
$sql = "
    SELECT id,title FROM `TABLE1` WHERE id IN($id)
    UNION
    SELECT id,title FROM `TABLE2` WHERE id IN($id)
    UNION
    ORDER BY id DESC
    LIMIT 0,10
";
$result = mysql_query($sql);

if( $result ){
    if( mysql_num_rows($result) ){
        $a = array();
        while($arr = mysql_fetch_array($result)){
            $table_name = mysql_field_table($result,0);
            echo "<br />表名:$table_name";
            //***就是在这里
            //***请问各位,怎么获取多个表名
        }
    }
}</code>

——————
Salute,
Thank you!

The result set after union is regarded as the same database table, so no matter how you get mysql_field_table here, you can only get one table name. The correct approach is that when you construct a SQL statement, attach the name of the database table it comes from to each record, so that you can know which database table this data comes from from the result set.

<code>$sql = "
    SELECT id,title,'TABLE1' AS ComesFrom FROM `TABLE1` WHERE id IN($id)
    UNION
    SELECT id,title,'TABLE2'              FROM `TABLE2` WHERE id IN($id)
    UNION
    ORDER BY id DESC
    LIMIT 0,10
";</code>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn