从两个表中选择,其中每个表中的不同列等于 $id ORDER BY 公共列 (PHP/MySQL)
我正在尝试从两个表中进行选择并按日期排序(它们都有的列)。一个表(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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试使用
UNION
:或者,如果您想在 tableA 和 tableB 之间保留重复项,请改用
UNION ALL
。编辑,根据您的评论,我知道您需要一列来指示该行来自哪个表。您可以在选择中添加一个静态列,如下所示:
这将为您提供一个如下形式的漂亮表格:
这就是您想要的,不是吗?理解你真正想通过这个实现什么有点困难......
Try using
UNION
: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:
This gives you a nice table on the following form:
That does what you want, doesn't it? It's a bit difficult understanding what you are really trying to achieve with this...