对多个表进行排序 MYSQL DateStamp
我正在构建一个简单的买卖网站,并希望在第一页上列出最新的广告。在我的数据库中有 4 个表。 用户
、汽车
、电子
和计算机
。
三个项目表(CARS
、ELECTRONICS
和 COMPUTERS
)中的每一个都有 DATESTAMP
,但我可以无法使用 MySQL 和 PHP 在此 DATESTAMP
之后轻松对它们进行排序。
它们都包含不同的列,但具有共同的 DATESTAMP
和 SSN
(标识用户如何拥有广告)。
编辑:
我尝试过加入表格,使用 php 中的 strtotime 对它们进行排序。但我似乎无法使用语法。玩过这段代码..但它变得非常复杂,我希望我可以用纯 SQL 更容易地完成它。它并不完整,但是..你可以看到我在想什么..
<?php
mysql_connect("localhost","root","");
mysql_select_db("project");
$SSN = utf8_decode(strip_tags($_GET['ssn']));
//firstname,lastname, email, phone, address, zipcode, district
$result = mysql_query("SELECT *
FROM CARS
WHERE CARS.SSN = '$SSN'");
$result2 = mysql_query("SELECT *
FROM ELECTRONICS
WHERE ELECTRONICS s.SSN = '$SSN'");
if (!$result) {
echo 'Could not run query: ' . mysql_error();
exit;
}
while($obj = mysql_fetch_object($result)) {
$arr[] = $obj;
}
echo '{"users":'.json_encode($arr).'}';
if (!$result2) {
echo 'Could not run query: ' . mysql_error();
exit;
}
while($obj = mysql_fetch_object($result2)) {
$arr[] = $obj;
}
echo '{"users":'.json_encode($arr).'}';
function mysort($a, $b) {
return(strtotime($b['datePosted']) - strtotime($a['datePosted']));
}
// pre-sort:
echo "<pre>Before:\n";
print_r($arr);
// do the sort:
usort($arr, 'mysort');
// show the result:
echo "After:\n";
print_r($arr);
echo "</pre>";
?>
I'm building a simple sell&buy-site and want to list the latest ads on the first pages. In my database I have 4 tables. USERS
, CARS
, ELECTRONICS
and COMPUTERS
.
Each of the three tables of items (CARS
, ELECTRONICS
and COMPUTERS
) have DATESTAMP
in them, but I can't manage to easily sort them after this DATESTAMP
using MySQL and PHP.
They all contain different columns but have DATESTAMP
and SSN
(identifying the user how owns the ad) in common.
EDIT:
I've tried Joining the tables, sorting them using strtotime in php.. but I can't seem to get the syntax to play along. Played with this code.. but It got very complicated and I was hoping I could do it easier in pure SQL.It's not complete but.. you can see what I was thinking..
<?php
mysql_connect("localhost","root","");
mysql_select_db("project");
$SSN = utf8_decode(strip_tags($_GET['ssn']));
//firstname,lastname, email, phone, address, zipcode, district
$result = mysql_query("SELECT *
FROM CARS
WHERE CARS.SSN = '$SSN'");
$result2 = mysql_query("SELECT *
FROM ELECTRONICS
WHERE ELECTRONICS s.SSN = '$SSN'");
if (!$result) {
echo 'Could not run query: ' . mysql_error();
exit;
}
while($obj = mysql_fetch_object($result)) {
$arr[] = $obj;
}
echo '{"users":'.json_encode($arr).'}';
if (!$result2) {
echo 'Could not run query: ' . mysql_error();
exit;
}
while($obj = mysql_fetch_object($result2)) {
$arr[] = $obj;
}
echo '{"users":'.json_encode($arr).'}';
function mysort($a, $b) {
return(strtotime($b['datePosted']) - strtotime($a['datePosted']));
}
// pre-sort:
echo "<pre>Before:\n";
print_r($arr);
// do the sort:
usort($arr, 'mysort');
// show the result:
echo "After:\n";
print_r($arr);
echo "</pre>";
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从设计的角度来看,您可以拥有一个 ITEMS 表,其中包含与所有类型的项目相关的属性,例如 ID、DATESTAMP、SSN。每个其他项目相关表上的 ID PK 与 ITEMS 表中相关项目的 ID 相匹配。
From a design standpoint, you could have an ITEMS table that contains attributes that relate to all types of item, for example ID, DATESTAMP, SSN. Have ID PKs on each of the other item-related tables that match the ID of the relevant item from the ITEMS table.