Impala中的limit子句用于將結(jié)果集的行數(shù)限制為所需的數(shù),即查詢的結(jié)果集不包含超過(guò)指定限制的記錄。
以下是Impala中Limit子句的語(yǔ)法。
select * from table_name order by id limit numerical_expression;
假設(shè)我們?cè)跀?shù)據(jù)庫(kù)my_db中有一個(gè)名為customers的表,其內(nèi)容如下 -
[quickstart.cloudera:21000] > select * from customers; Query: select * from customers +----+----------+-----+-----------+--------+ | id | name | age | address | salary | +----+----------+-----+-----------+--------+ | 3 | kaushik | 23 | Kota | 30000 | | 6 | Komal | 22 | MP | 32000 | | 1 | Ramesh | 32 | Ahmedabad | 20000 | | 5 | Hardik | 27 | Bhopal | 40000 | | 2 | Khilan | 25 | Delhi | 15000 | | 8 | ram | 22 | vizag | 31000 | | 9 | robert | 23 | banglore | 28000 | | 7 | ram | 25 | chennai | 23000 | | 4 | Chaitali | 25 | Mumbai | 35000 | +----+----------+-----+-----------+--------+ Fetched 9 row(s) in 0.51s
您可以使用order by子句按照id的升序排列表中的記錄,如下所示。
[quickstart.cloudera:21000] > select * from customers order by id; Query: select * from customers order by id +----+----------+-----+-----------+--------+ | id | name | age | address | salary | +----+----------+-----+-----------+--------+ | 1 | Ramesh | 32 | Ahmedabad | 20000 | | 2 | Khilan | 25 | Delhi | 15000 | | 3 | kaushik | 23 | Kota | 30000 | | 4 | Chaitali | 25 | Mumbai | 35000 | | 5 | Hardik | 27 | Bhopal | 40000 | | 6 | Komal | 22 | MP | 32000 | | 7 | ram | 25 | chennai | 23000 | | 8 | ram | 22 | vizag | 31000 | | 9 | robert | 23 | banglore | 28000 | +----+----------+-----+-----------+--------+ Fetched 9 row(s) in 0.54s
現(xiàn)在,使用limit子句,您可以將輸出的記錄數(shù)限制為4,使用limit子句如下所示。
[quickstart.cloudera:21000] > select * from customers order by id limit 4;
執(zhí)行時(shí),上述查詢給出以下輸出。
Query: select * from customers order by id limit 4 +----+----------+-----+-----------+--------+ | id | name | age | address | salary | +----+----------+-----+-----------+--------+ | 1 | Ramesh | 32 | Ahmedabad | 20000 | | 2 | Khilan | 25 | Delhi | 15000 | | 3 | kaushik | 23 | Kota | 30000 | | 4 | Chaitali | 25 | Mumbai | 35000 | +----+----------+-----+-----------+--------+ Fetched 4 row(s) in 0.64s
更多建議: