---
title: "数据库查询语句(高端)"
author: "J2EE大鸟"
date: "2013-05-23T16:04:22Z"
tags: []
categories: ["数据库"]
slug: "untitled"
draft: false
lastmod: "2013-05-23T16:04:22Z"
id: 1874149
url: "https://benworld.iteye.com/blog/1874149"
---

1\. 想要查出在订单这张表中，按起始港出现频率由高到低的订单排序

select o.* from orders o orderby (select count(os.shid) from orders os where os.shid = o.shid)

 

2\. 想要查出在订单表中，出现频率最高的起始港的港口信息

SELECT h.\*, orderharbor.count from harbor h INNER JOIN 

(SELECT o.shid,COUNT(o.shid) as count from orders o GROUP BY o.shid ORDER BY count DESC LIMIT 1) orderharbor

ON h.harborid = orderharbor.shid

 

3\. 查询表中最小值属性的一般方法： 将记录按该属性的升序排列，取出第一个记录即可。

 

4\. union 会消除重复数据。如果想在结果保留重复数据，可以使用union all。

    intersect 操作输入两个关系A和B，选择那些即在A也在B的行。

    except 操作输入两个关系A和B，找出所有在A但不在B的行。

 

5\. is null 或者is not null 操作符检测null是否存在。