嵌套查询,子查询SQL

时间:2017-01-10 04:01:29

标签: sql postgresql greatest-n-per-group

我有两张桌子:

<?php
# Adding just the config will take care of including classes for you,
# provided you have set up the classes into a class folder and the autoloader
# has the correct path in place. This would be at the top of every page
require_once(__DIR__.DIRECTORY_SEPARATOR.'config.php');
# Create the bookings here, pass the database. It may be beneficial to make the
# connection in the config file for site use, but I don't know your site
# structure, so that is a guess...
$con = (new Database())->connect();
$newbookings = new Booking($con);
?>   
<div class="container">
    <table class="table table-hover text-center">
        <thead>
            <tr align="center">
                <th>Name</th>
                <th>Contact</th>
                <th>Email</th>
            </tr>
        </thead>
        <tbody>
            <?php
            # Since the class already returns an array, just use foreach()
            foreach($newbookings->showBooking() as $row){
            ?>
            <tr>
                <td><?php $row['name'];?></td>
                <td><?php $row['contact'];?></td>
                <td><?php $row['email'];?></td>
            </tr>
            <?php   
            }
            ?>  
        </tbody>
    </table>
</div>
select * from friends;
 user_id | friend_id |         created_at         
---------+-----------+----------------------------
 user    | user2     | 2017-01-09 21:29:56.31972
 user    | user3     | 2017-01-09 21:29:58.567925
 user    | user4     | 2017-01-09 21:30:01.200806
 user2   | user      | 2017-01-09 21:30:49.498912
 user3   | user      | 2017-01-09 21:30:53.549042
 user4   | user      | 2017-01-09 21:30:56.519135

此SQL语句成功检索到一个用户的最后一条消息。例如。 user3发送给用户的最后一条消息

    id | user_id | user_from | message 
----+---------+-----------+---------
  1 | user    | user2     | hey
  2 | user    | user3     | de
  3 | user    | user4     | bal
  4 | user    | user3     | ok
  5 | user    | user3     | nice
  6 | user    | user4     | cana
  7 | user    | user2     | doc
select id, user_from, message 
from messages 
WHERE user_id = 'user' 
AND user_from = 'user3' 
ORDER BY id DESC 
limit 1;

我需要检索一组邮件。每条消息代表朋友发送给用户的最后一条消息

我需要的是组合一个查询,以便我得到类似的内容

 id | user_from | message 
----+---------+---------
  5 | user3    | nice

3 个答案:

答案 0 :(得分:0)

distinct on是最简单的解决方案。

select DISTINCT ON(user_from) id, user_from, message 
from messages WHERE user_id = 'user'
ORDER BY user_from,id DESC;

这使用了对SQL标准的postresql扩展,这也是使用窗口函数执行此操作的标准方法,但它更复杂,并且使用自联接可能效率低下。< / p>

答案 1 :(得分:0)

您可以使用加入和窗口功能(对于您的情况为linear-gradient)。

假设第一个表是var str1 = 'i love javascript'; var str2 = 'i love javascripttt'; var matchPer = match(str1,str2); // result might be 80% , 85%, 90% ,95% etc 而第二个表是row_number,则此解决方案可能有效(尚未测试)。

friends

答案 2 :(得分:-1)

首先规范化数据库,关系数据库需要这个。

我不确定你对db design和sql有多熟悉。但是请看一下使用连接,这对于规范化的数据库更好;)

在这里阅读关于规范化的内容 http://www.studytonight.com/dbms/database-normalization.php

还可以查看w3schools或tutorialspoint上的联接,你会得到你需要的东西