对多个表进行排序 MYSQL DateStamp

发布于 2024-11-07 00:14:13 字数 1734 浏览 2 评论 0原文

我正在构建一个简单的买卖网站,并希望在第一页上列出最新的广告。在我的数据库中有 4 个表。 用户汽车电子计算机

三个项目表(CARSELECTRONICSCOMPUTERS)中的每一个都有 DATESTAMP ,但我可以无法使用 MySQL 和 PHP 在此 DATESTAMP 之后轻松对它们进行排序。

它们都包含不同的列,但具有共同的 DATESTAMPSSN(标识用户如何拥有广告)。

编辑:

我尝试过加入表格,使用 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 技术交流群。

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

发布评论

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

评论(1

从来不烧饼 2024-11-14 00:14:13

从设计的角度来看,您可以拥有一个 ITEMS 表,其中包含与所有类型的项目相关的属性,例如 ID、DATESTAMP、SSN。每个其他项目相关表上的 ID PK 与 ITEMS 表中相关项目的 ID 相匹配。

ITEMS

ID  DATESTAMP  SSN
1   2011-01-01 12345
2   2011-01-02 12345
3   2011-01-04 54321

CARS

ID MANUFACTURER MODEL 
1  Volvo        V40
3  Volkswagen   Beetle

COMPUTERS

ID BRAND PROCESSOR
2  Dell  386

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.

ITEMS

ID  DATESTAMP  SSN
1   2011-01-01 12345
2   2011-01-02 12345
3   2011-01-04 54321

CARS

ID MANUFACTURER MODEL 
1  Volvo        V40
3  Volkswagen   Beetle

COMPUTERS

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