是否可以像标题中描述的那样进行查询?我有两个具有不同类型列的表,它们之间没有任何相似之处。我想要做的是将它们连接在一起并以最早的顺序显示它们,但限制为每页5个。我得到项目数量的方式是这样的:
$limit = 5;
$page = 1;
if(isset($_GET['pn'])) {
$page = $_GET['pn'];
$lim = ($page-1)*$limit;
}else{
$lim = ($page-1)*$limit;
}
$l = mysqli_query($mysqli_connect, "SELECT id FROM table1");
$s = mysqli_query($mysqli_connect, "SELECT id FROM table2");
$r = mysqli_num_rows($l) + mysqli_num_rows($s);
然后我只是在用户点击下一步时在网址中添加页码。现在从一个表中获取查询我正在这样做:
$sql = mysqli_query($mysqli_connect, "SELECT * FROM table1 ORDER BY dateadded desc LIMIT $lim, $limit");
我需要帮助构建一个将同时选择两个表的查询...以及如何使用LIMIT
跟踪已添加的行?
更新:
$sql = mysqli_query($mysqli_connect, "SELECT id FROM (SELECT id, dateadded FROM table1 UNION SELECT id, dateadded FROM table2) ORDER BY dateadded LIMIT $lim, $limit");
$num_rows = mysqli_num_rows($sql);
echo $num_rows;
我收到mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in...
的警告。我不能看到有多少物品被退回???
答案 0 :(得分:2)
我假设你的桌子至少有一个id和一个带日期(dateadded)的字段
首先,我会检索您需要在第一页上显示这样的查询的ID。
SELECT id FROM (
SELECT id, dateadded FROM table1
UNION
SELECT id, dateadded FROM table2
) AS mergedtable ORDER BY dateadded LIMIT 0, 5;
根据检索到的ID,您可以查询数据库以获取表中的所有信息
SELECT * FROM table1 WHERE id IN ( /* list of ids */ );
SELECT * FROM table2 WHERE id IN ( /* list of ids */ );
然后你必须做一个php操作来合并你的结果和订单然后根据他们的日期。如果它们基于两个不同的序列
,则可能还需要根据它们的原始表为它们添加前缀答案 1 :(得分:0)
SELECT id FROM (SELECT id, date FROM table1 UNION ALL SELECT id FROM table2) ORDER BY date