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

发布于 2024-11-30 05:50:26 字数 1476 浏览 1 评论 0原文

我正在尝试从两个表中进行选择并按日期排序(它们都有的列)。一个表(tableA)有一个名为“A”的列,另一个表(tableB)有一个名为“B”的列,我使用 array_key_exists() 来区分两者(如果“A”键存在,我通过FunctionA(),如果“B”键存在,我通过 FunctionB() 运行数组。我只需要 20 个最新(按日期)的条目。我需要 SQL 查询来完成此任务。

我已经知道回复将是“如果它们的结构相似,那么您应该只使用单个表”,但我不想这样做,因为 tableA 与 tableB 截然不同(tableA 中有更多列),使用单个表来存储数据会导致为 tableB 格式化的条目出现大量空列,更不用说由于 tableB 不需要 tableA 的大部分列,这将是一种非常难看的表格式。

我只想以有序(按日期)的方式在一个流中显示两个表中的数据。

顺便说一下,我需要 SELECT WHERE tableA.poster_id = $id 和 tableB.receiver_id = $id 。

解决方案:

我正在更新此内容,以防其他人遇到同样的困境。在实现了 @Erik A. Brandstadmoen 慷慨地给我的 SQL 查询之后,我的代码基本上是这样的:

$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++;
}

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++;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风吹过旳痕迹 2024-12-07 05:50:26

尝试使用 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

或者,如果您想在 tableA 和 tableB 之间保留重复项,请改用 UNION ALL

编辑,根据您的评论,我知道您需要一列来指示该行来自哪个表。您可以在选择中添加一个静态列,如下所示:

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

这将为您提供一个如下形式的漂亮表格:

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

这就是您想要的,不是吗?理解你真正想通过这个实现什么有点困难......

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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文