如何选择那些记录只是前7

时间:2017-07-10 16:55:18

标签: mysql select

我有一张表t1喜欢

mysql> select * from t1;
+------+-------+------+
| id   | level | gap  |
+------+-------+------+
|    1 |     6 |   50 |
|    1 |     5 |   10 |
|    2 |     5 |   12 |
|    2 |     5 |   10 |
|    3 |     8 |    4 |
|    3 |     9 |    1 |
|    3 |     9 |    3 |
|    3 |     7 |    2 |
|    4 |     8 |   10 |
|    4 |     8 |    2 |
|    4 |     9 |    6 |
|    4 |     9 |    4 |
+------+-------+------+

当然我可以用

select * from t1 where id=3;

获取id=3的所有记录:

mysql> select * from t1 where id=3;
+------+-------+------+
| id   | level | gap  |
+------+-------+------+
|    3 |     8 |    4 |
|    3 |     9 |    1 |
|    3 |     9 |    3 |
|    3 |     7 |    2 |
+------+-------+------+

但我希望第一个结果是7.我的意思是它应该是红色矩形部分

如何编写这样的命令?

2 个答案:

答案 0 :(得分:2)

内部选择获得前7条记录 - 不要忘记添加@IBAction func searchButtonTaped(sender:UIBarButtonItem){ print("tap inside") self.navigationItem.titleView = searchController.searchBar searchController.searchBar.setShowsCancelButton(false, animated: false) self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Cancelar", style: .plain, target: self, action: #selector(hideSearchBar(sender:))) //self.searchIcon.is //self.navigationItem.rightBarButtonItem?.action = #selector(hideSearchBar(:)) // searchController.searchBar.touchesCancelled(UITouch, with: .touchUpInside){ //} //self.navigationItem.rightBarButtonItem. } func hideSearchBar(sender:UIBarButtonItem) { print("hay que escpder") self.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.search, target: self, action: #selector(searchButtonTaped(sender:))) //logoImageView.alpha = 0 UIView.animate(withDuration: 0.3, animations: { self.navigationItem.titleView = nil// = self.logoImageView self.navigationItem.title = "Noticias" //self.logoImageView.alpha = 1 }, completion: { finished in }) } 以获得保证结果

order by

答案 1 :(得分:0)

如果架构是这样组织的,您可以尝试:

SELECT * FROM (SELECT * FROM t1 LIMIT 7) WHERE id=3