1

我有 2 个具有相同字段名称的表,我想显示来自两个字段列的记录。

Table_a
    id date          user    test
    1   2013-08-08   Adam    Various    
    2   2013-08-08   Paul    Record

    Table_b

      id date          user    test
        1   2013-08-08   Adam    Record    
        2   2013-08-08   Paul    Mytest


    $sql = "Select Table_a.date, Table_a.user, Table_a.test, Table_b.date, Table_b.user, Table_b.test FROM table_a, Table_b where Table_a.user like table_b AND table_a.Date = table_b.date AND table_b.Date = '".date("Y-m-d", strtotime("today"))."'";
    $result = mysql_query($sql);
    $row = mysql_fetch_assoc($result); 


 echo "<p'><b>" . $row['user'] . "</b></p>";
 echo "<p><b>". $row['test'] ."</b></p>";
 echo "<p><b>". $row['test'] ."</b></p>";

例如,我希望结果是 Adam -various -record。也就是说,我希望第三个回"<p><b>". $row['test'] ."</b></p>";显显示来自 table_b 的记录。

谢谢

4

3 回答 3

5

您可以使用 SQL 别名

SELECT column_name AS alias_name

您的代码中的示例

$sql = "SELECT Table_a.date AS tableA_date, 
               Table_a.user AS tableA_user,
               Table_a.test AS tableA_test,
               Table_b.date AS tableB_date,
               Table_b.user AS tableB_user, 
               Table_b.test AS tableB_test
        FROM Table_a AS tableA,
             Table_b AS tableB
        WHERE tableA_user
        LIKE tableB 
        AND tableA_date = tableB_date
        AND tableD_date = '".date("Y-m-d", strtotime("today"))."'";
于 2013-08-07T06:32:35.363 回答
0

尝试这个:

$sql = "Select
              a.date as adate,
              a.user as auser,
              a.test as atest,
              b.date as bdate,
              b.user as buser,
              b.test as btest
        FROM table_a a, Table_b b
        where 
              a.user like b.user
              AND a.Date = b.date
              AND b.Date = '".date("Y-m-d", strtotime("today"))."'";
echo "<p'><b>" . $row['auser'] . "</b></p>";
echo "<p><b>". $row['atest'] ."</b></p>";
echo "<p><b>". $row['btest'] ."</b></p>";

请注意,我已经写了a.user like b.user. a.user like b是错的。

于 2013-08-07T06:41:11.347 回答
0

使用以下查询

Select table_a.user,table_a.test,table_b.test FROM table_a left join table_b on
table_a.user=table_b.user where table_a.Date = table_b.date AND table_b.Date = curdate()

它将返回如下结果

在此处输入图像描述

认为它会解决你的问题。

于 2013-08-07T06:59:59.400 回答