douxian4376 2011-08-17 17:52
浏览 20
已采纳

从两个表中选择WHERE每个表中的不同列等于$ id ORDER BY公共列(PHP / MySQL)

I'm trying to SELECT from two tables and ORDER BY date (a column they both have). One table (tableA) has a column called "A" and the other table (tableB) has a column called "B", I use array_key_exists() to differentiate between the two (If "A" key exists, I run the array through FunctionA(), if "B" key exists, I run the array through FunctionB()). I only need the 20 latest (date wise) entries. I need the SQL Query to accomplish this.

I already know a reply will be "if they're similarly structured, then you should just use a single table", but I don't want to do that because tableA is drastically different from tableB (a lot more columns in tableA), and using a single table to store the data would result in a LOT of empty columns for entries formatted for tableB, not to mention it'd be a very ugly looking table format due to tableB not needing the majority of tableA's columns).

I just want to display data from both tables in an ordered (by date) fashion, and in one single stream.

I need to SELECT WHERE tableA.poster_id = $id and tableB.receiver_id = $id by the way.

SOLUTION:

I'm updating this just in case anyone else with the same dilemma comes along. After implementing the SQL query that @Erik A. Brandstadmoen had graciously given me, this is basically what my code ended up as:

$MySQL->SQL("SELECT * FROM
              (SELECT A.id AS id, A.date AS date, 'tableA' AS source 
                 FROM tableA A WHERE A.poster_id = $id 
                    UNION
                      SELECT B.id AS id, B.date AS date, 'tableB' AS source
                           FROM tableB B WHERE B.receiver_id = $id) AS T
                               ORDER BY T.date DESC LIMIT 0, 20");

$GetStream = array();
$i = 0;
while ($row = mysql_fetch_array($MySQL->Result))
{
    $GetStream[$i]['id'] = $row['id'];
    $GetStream[$i]['date']=$row['date'];
    $GetStream[$i]['source'] = $row['source'];
    $i++;
}

*** later on down the code ***

$i = 0;
while ($i<count($GetStream))
{
    if ($GetStream[$i]['source'] == "tableA")
    { 
        FunctionA($GetStream[$i]);
    }
    else
    {
        FunctionB($GetStream[$i]);
    }
    $i++;
}
  • 写回答

1条回答 默认 最新

  • dongxu6418 2011-08-17 18:00
    关注

    Try using UNION:

    SELECT * FROM (
    SELECT A.col1 AS x, A.col2 As y, A.col3 AS date FROM tableA A
    WHERE tableA.poster_id = $id
    UNION 
    SELECT B.colA AS x, B.colB AS y, B.colC AS date FROM  tableB B
    WHERE tableB.receiver_id = $id
    )
    ORDER BY date DESC
    LIMIT 0, 20
    

    OR, IF you would like to keep duplicates between tableA and tableB, use UNION ALL instead.

    EDIT, according to your comments, I understand that you need a column indicating which table the row is from. You can just add a static column in the select, like this:

    SELECT * FROM (
    SELECT A.col1 AS x, A.col2 As y, A.col3 AS date, 'A' as source FROM tableA A
    WHERE tableA.poster_id = $id
    UNION 
    SELECT B.colA AS x, B.colB AS y, B.colC AS date, 'B' as source FROM  tableB B
    WHERE tableB.receiver_id = $id
    )
    ORDER BY date DESC
    LIMIT 0, 20
    

    This gives you a nice table on the following form:

    x    y    date    source
    =========================
    (v1) (v2) (d1)    'A'
    (v3) (v4) (d2)    'B'
    (v1) (v2) (d3)    'B'
    (v3) (v4) (d4)    'A'
    

    That does what you want, doesn't it? It's a bit difficult understanding what you are really trying to achieve with this...

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比