第二章 进入SQL兼容性测试

查询数据

1 DISTINCT关键字

Mysql:

  1. mysql> select DISTINCT title_id from titles;
  2. +----------+
  3. | title_id |
  4. +----------+
  5. | BU1032 |
  6. | BU1111 |
  7. | BU2075 |
  8. | BU7832 |
  9. | MC2222 |
  10. | MC3021 |
  11. | MC3026 |
  12. | PC1035 |
  13. | PC8888 |
  14. | PC9999 |
  15. | PS1372 |
  16. | PS2091 |
  17. | PS2106 |
  18. | PS3333 |
  19. | PS7777 |
  20. | TC3218 |
  21. | TC4203 |
  22. | TC7777 |
  23. +----------+
  24. 18 rows in set (0.00 sec)

Inceptor:

  1. 0: jdbc:hive2://192.168.1.70:10000/> select DISTINCT title_id from titles;
  2. +-----------+
  3. | title_id |
  4. +-----------+
  5. | BU7832 |
  6. | BU2075 |
  7. | MC3026 |
  8. | MC2222 |
  9. | PS7777 |
  10. | PS1372 |
  11. | PC8888 |
  12. | TC7777 |
  13. | BU1032 |
  14. | MC3021 |
  15. | PC9999 |
  16. | TC3218 |
  17. | PC1035 |
  18. | BU1111 |
  19. | PS2091 |
  20. | PS3333 |
  21. | PS2106 |
  22. | TC4203 |
  23. +-----------+
  24. 18 rows selected (7.386 seconds)

2 限制结果

Mysql:

  1. mysql> select title from titles limit 5;
  2. +-----------------------------------------------------------------+
  3. | title |
  4. +-----------------------------------------------------------------+
  5. | But Is It User Friendly? |
  6. | Computer Phobic AND Non-Phobic Individuals: Behavior Variations |
  7. | Cooking with Computers: Surreptitious Balance Sheets |
  8. | Emotional Security: A New Algorithm |
  9. | Fifty Years in Buckingham Palace Kitchens |
  10. +-----------------------------------------------------------------+
  11. 5 rows in set (0.00 sec)

Inceptor:

  1. 0: jdbc:hive2://192.168.1.70:10000/> select title from titles limit 5;
  2. +-------------------------------------------------------+
  3. | title |
  4. +-------------------------------------------------------+
  5. | The Busy Executive's Database Guide |
  6. | Cooking with Computers: Surreptitious Balance Sheets |
  7. | You Can Combat Computer Stress! |
  8. | Straight Talk About Computers |
  9. | Silicon Valley Gastronomic Treats |
  10. +-------------------------------------------------------+
  11. 5 rows selected (1.918 seconds)

排序查询数据

1 Order语句:

Mysql:

  1. mysql> select title,price from titles order by price;
  2. +-----------------------------------------------------------------+---------+
  3. | title | price |
  4. +-----------------------------------------------------------------+---------+
  5. | Net Etiquette | NULL |
  6. | The Psychology of Computer Cooking | NULL |
  7. | You Can Combat Computer Stress! | 2.9900 |
  8. | The Gourmet Microwave | 2.9900 |
  9. | Life Without Fear | 7.0000 |
  10. | Emotional Security: A New Algorithm | 7.9900 |
  11. | Is Anger the Enemy? | 10.9500 |
  12. | Cooking with Computers: Surreptitious Balance Sheets | 11.9500 |
  13. | Fifty Years in Buckingham Palace Kitchens | 11.9500 |
  14. | Sushi, Anyone? | 14.9900 |
  15. | Prolonged Data Deprivation: Four Case Studies | 19.9900 |
  16. | Silicon Valley Gastronomic Treats | 19.9900 |
  17. | Straight Talk About Computers | 19.9900 |
  18. | The Busy Executive's Database Guide | 19.9900 |
  19. | Secrets of Silicon Valley | 20.0000 |
  20. | Onions, Leeks, and Garlic: Cooking Secrets of the Mediterranean | 20.9500 |
  21. | Computer Phobic AND Non-Phobic Individuals: Behavior Variations | 21.5900 |
  22. | But Is It User Friendly? | 22.9500 |
  23. +-----------------------------------------------------------------+---------+
  24. 18 rows in set (0.00 sec)
  25. mysql> select title,price from titles order by price desc;
  26. +-----------------------------------------------------------------+---------+
  27. | title | price |
  28. +-----------------------------------------------------------------+---------+
  29. | But Is It User Friendly? | 22.9500 |
  30. | Computer Phobic AND Non-Phobic Individuals: Behavior Variations | 21.5900 |
  31. | Onions, Leeks, and Garlic: Cooking Secrets of the Mediterranean | 20.9500 |
  32. | Secrets of Silicon Valley | 20.0000 |
  33. | The Busy Executive's Database Guide | 19.9900 |
  34. | Prolonged Data Deprivation: Four Case Studies | 19.9900 |
  35. | Straight Talk About Computers | 19.9900 |
  36. | Silicon Valley Gastronomic Treats | 19.9900 |
  37. | Sushi, Anyone? | 14.9900 |
  38. | Cooking with Computers: Surreptitious Balance Sheets | 11.9500 |
  39. | Fifty Years in Buckingham Palace Kitchens | 11.9500 |
  40. | Is Anger the Enemy? | 10.9500 |
  41. | Emotional Security: A New Algorithm | 7.9900 |
  42. | Life Without Fear | 7.0000 |
  43. | The Gourmet Microwave | 2.9900 |
  44. | You Can Combat Computer Stress! | 2.9900 |
  45. | The Psychology of Computer Cooking | NULL |
  46. | Net Etiquette | NULL |
  47. +-----------------------------------------------------------------+---------+
  48. 18 rows in set (0.00 sec)

Inceptor:

  1. 0: jdbc:hive2://192.168.1.70:10000/> select title,price from titles order by price;
  2. +------------------------------------------------------------------+---------------+
  3. | title | price |
  4. +------------------------------------------------------------------+---------------+
  5. | Sushi | 0877 |
  6. | Is Anger the Enemy? | 10.9500 |
  7. | Cooking with Computers: Surreptitious Balance Sheets | 11.9500 |
  8. | Fifty Years in Buckingham Palace Kitchens | 11.9500 |
  9. | The Busy Executive's Database Guide | 19.9900 |
  10. | Straight Talk About Computers | 19.9900 |
  11. | Silicon Valley Gastronomic Treats | 19.9900 |
  12. | Prolonged Data Deprivation: Four Case Studies | 19.9900 |
  13. | You Can Combat Computer Stress! | 2.9900 |
  14. | The Gourmet Microwave | 2.9900 |
  15. | Secrets of Silicon Valley | 20.0000 |
  16. | Computer Phobic AND Non-Phobic Individuals: Behavior Variations | 21.5900 |
  17. | But Is It User Friendly? | 22.9500 |
  18. | Life Without Fear | 7.0000 |
  19. | Emotional Security: A New Algorithm | 7.9900 |
  20. | The Psychology of Computer Cooking | null |
  21. | Net Etiquette | null |
  22. | Onions | trad_cook |
  23. +------------------------------------------------------------------+---------------+
  24. 18 rows selected (1.993 seconds)

过滤数据

Mysql:

  1. mysql> select title from titles where price>=14;
  2. +-----------------------------------------------------------------+
  3. | title |
  4. +-----------------------------------------------------------------+
  5. | The Busy Executive's Database Guide |
  6. | Straight Talk About Computers |
  7. | Silicon Valley Gastronomic Treats |
  8. | But Is It User Friendly? |
  9. | Secrets of Silicon Valley |
  10. | Computer Phobic AND Non-Phobic Individuals: Behavior Variations |
  11. | Prolonged Data Deprivation: Four Case Studies |
  12. | Onions, Leeks, and Garlic: Cooking Secrets of the Mediterranean |
  13. | Sushi, Anyone? |
  14. +-----------------------------------------------------------------+
  15. 9 rows in set (0.00 sec)
  16. mysql> select title from titles where price between 5 and 10;
  17. +-------------------------------------+
  18. | title |
  19. +-------------------------------------+
  20. | Life Without Fear |
  21. | Emotional Security: A New Algorithm |
  22. +-------------------------------------+
  23. 2 rows in set (0.00 sec)
  24. mysql> select title from titles where price IS NULL;
  25. +------------------------------------+
  26. | title |
  27. +------------------------------------+
  28. | The Psychology of Computer Cooking |
  29. | Net Etiquette |
  30. +------------------------------------+
  31. 2 rows in set (0.00 sec)

inceptor:

  1. 0: jdbc:hive2://192.168.1.70:10000/> select title from titles where price>=14;
  2. +------------------------------------------------------------------+
  3. | title |
  4. +------------------------------------------------------------------+
  5. | The Busy Executive's Database Guide |
  6. | Straight Talk About Computers |
  7. | Silicon Valley Gastronomic Treats |
  8. | But Is It User Friendly? |
  9. | Secrets of Silicon Valley |
  10. | Computer Phobic AND Non-Phobic Individuals: Behavior Variations |
  11. | Prolonged Data Deprivation: Four Case Studies |
  12. | Sushi |
  13. +------------------------------------------------------------------+
  14. 8 rows selected (7.862 seconds)
  15. 0: jdbc:hive2://192.168.1.70:10000/> select title from titles where price between 5 and 10;
  16. +--------------------------------------+
  17. | title |
  18. +--------------------------------------+
  19. | Life Without Fear |
  20. | Emotional Security: A New Algorithm |
  21. +--------------------------------------+
  22. 2 rows selected (1.413 seconds)
  23. 0: jdbc:hive2://192.168.1.70:10000/> select title,price from titles where price='null' ;
  24. +-------------------------------------+--------+
  25. | title | price |
  26. +-------------------------------------+--------+
  27. | The Psychology of Computer Cooking | null |
  28. | Net Etiquette | null |
  29. +-------------------------------------+--------+
  30. 2 rows selected (1.162 seconds)

AND操作符

MYSQL:

  1. mysql> select title,title_id,price,pubdate from titles
  2. -> where title_id='BU1032' and price>10;
  3. +-------------------------------------+----------+---------+---------------------+
  4. | title | title_id | price | pubdate |
  5. +-------------------------------------+----------+---------+---------------------+
  6. | The Busy Executive's Database Guide | BU1032 | 19.9900 | 1991-06-12 00:00:00 |
  7. +-------------------------------------+----------+---------+---------------------+
  8. 1 row in set (0.00 sec)

Inceptor:

  1. 0: jdbc:hive2://192.168.1.70:10000/> select title,title_id,price,pubdate from titles
  2. . . . . . . . . . . . . . . . . . .> where title_id='BU1032' and price>10;
  3. +--------------------------------------+-----------+----------+------------------------+
  4. | title | title_id | price | pubdate |
  5. +--------------------------------------+-----------+----------+------------------------+
  6. | The Busy Executive's Database Guide | BU1032 | 19.9900 | 1991-06-12 00:00:00.0 |
  7. +--------------------------------------+-----------+----------+------------------------+
  8. 1 row selected (1.153 seconds)

OR操作符

MYSQL:

  1. mysql> select title,title_id,price from titles where title_id='BU1032' or price>10;
  2. +-----------------------------------------------------------------+----------+---------+
  3. | title | title_id | price |
  4. +-----------------------------------------------------------------+----------+---------+
  5. | The Busy Executive's Database Guide | BU1032 | 19.9900 |
  6. | Cooking with Computers: Surreptitious Balance Sheets | BU1111 | 11.9500 |
  7. | Straight Talk About Computers | BU7832 | 19.9900 |
  8. | Silicon Valley Gastronomic Treats | MC2222 | 19.9900 |
  9. | But Is It User Friendly? | PC1035 | 22.9500 |
  10. | Secrets of Silicon Valley | PC8888 | 20.0000 |
  11. | Computer Phobic AND Non-Phobic Individuals: Behavior Variations | PS1372 | 21.5900 |
  12. | Is Anger the Enemy? | PS2091 | 10.9500 |
  13. | Prolonged Data Deprivation: Four Case Studies | PS3333 | 19.9900 |
  14. | Onions, Leeks, and Garlic: Cooking Secrets of the Mediterranean | TC3218 | 20.9500 |
  15. | Fifty Years in Buckingham Palace Kitchens | TC4203 | 11.9500 |
  16. | Sushi, Anyone? | TC7777 | 14.9900 |
  17. +-----------------------------------------------------------------+----------+---------+
  18. 12 rows in set (0.01 sec)

Inceptor:

  1. 0: jdbc:hive2://192.168.1.70:10000/> select title,title_id,price from titles where title_id='BU1032' or price>10;
  2. +------------------------------------------------------------------+-----------+----------+
  3. | title | title_id | price |
  4. +------------------------------------------------------------------+-----------+----------+
  5. | The Busy Executive's Database Guide | BU1032 | 19.9900 |
  6. | Cooking with Computers: Surreptitious Balance Sheets | BU1111 | 11.9500 |
  7. | Straight Talk About Computers | BU7832 | 19.9900 |
  8. | Silicon Valley Gastronomic Treats | MC2222 | 19.9900 |
  9. | But Is It User Friendly? | PC1035 | 22.9500 |
  10. | Secrets of Silicon Valley | PC8888 | 20.0000 |
  11. | Computer Phobic AND Non-Phobic Individuals: Behavior Variations | PS1372 | 21.5900 |
  12. | Is Anger the Enemy? | PS2091 | 10.9500 |
  13. | Prolonged Data Deprivation: Four Case Studies | PS3333 | 19.9900 |
  14. | Fifty Years in Buckingham Palace Kitchens | TC4203 | 11.9500 |
  15. | Sushi | TC7777 | 0877 |
  16. +------------------------------------------------------------------+-----------+----------+
  17. 11 rows selected (1.023 seconds)

IN操作符

Mysql:

  1. mysql> select title,title_id,price from titles where title_id in ('BU1032','BU1111');
  2. +------------------------------------------------------+----------+---------+
  3. | title | title_id | price |
  4. +------------------------------------------------------+----------+---------+
  5. | The Busy Executive's Database Guide | BU1032 | 19.9900 |
  6. | Cooking with Computers: Surreptitious Balance Sheets | BU1111 | 11.9500 |
  7. +------------------------------------------------------+----------+---------+
  8. 2 rows in set (0.00 sec)
  9. mysql> select title,title_id,price from titles where title_id not in ('BU1032','BU1111');
  10. +-----------------------------------------------------------------+----------+---------+
  11. | title | title_id | price |
  12. +-----------------------------------------------------------------+----------+---------+
  13. | You Can Combat Computer Stress! | BU2075 | 2.9900 |
  14. | Straight Talk About Computers | BU7832 | 19.9900 |
  15. | Silicon Valley Gastronomic Treats | MC2222 | 19.9900 |
  16. | The Gourmet Microwave | MC3021 | 2.9900 |
  17. | The Psychology of Computer Cooking | MC3026 | NULL |
  18. | But Is It User Friendly? | PC1035 | 22.9500 |
  19. | Secrets of Silicon Valley | PC8888 | 20.0000 |
  20. | Net Etiquette | PC9999 | NULL |
  21. | Computer Phobic AND Non-Phobic Individuals: Behavior Variations | PS1372 | 21.5900 |
  22. | Is Anger the Enemy? | PS2091 | 10.9500 |
  23. | Life Without Fear | PS2106 | 7.0000 |
  24. | Prolonged Data Deprivation: Four Case Studies | PS3333 | 19.9900 |
  25. | Emotional Security: A New Algorithm | PS7777 | 7.9900 |
  26. | Onions, Leeks, and Garlic: Cooking Secrets of the Mediterranean | TC3218 | 20.9500 |
  27. | Fifty Years in Buckingham Palace Kitchens | TC4203 | 11.9500 |
  28. | Sushi, Anyone? | TC7777 | 14.9900 |
  29. +-----------------------------------------------------------------+----------+---------+
  30. 16 rows in set (0.00 sec)

Inceptor:

  1. 0: jdbc:hive2://192.168.1.70:10000/> select title,title_id,price from titles where title_id in ('BU1032','BU1111');
  2. +-------------------------------------------------------+-----------+----------+
  3. | title | title_id | price |
  4. +-------------------------------------------------------+-----------+----------+
  5. | The Busy Executive's Database Guide | BU1032 | 19.9900 |
  6. | Cooking with Computers: Surreptitious Balance Sheets | BU1111 | 11.9500 |
  7. +-------------------------------------------------------+-----------+----------+
  8. 2 rows selected (1.017 seconds)
  9. 0: jdbc:hive2://192.168.1.70:10000/> select title,title_id,price from titles where title_id not in ('BU1032','BU1111');
  10. +------------------------------------------------------------------+-----------+---------------+
  11. | title | title_id | price |
  12. +------------------------------------------------------------------+-----------+---------------+
  13. | You Can Combat Computer Stress! | BU2075 | 2.9900 |
  14. | Straight Talk About Computers | BU7832 | 19.9900 |
  15. | Silicon Valley Gastronomic Treats | MC2222 | 19.9900 |
  16. | The Gourmet Microwave | MC3021 | 2.9900 |
  17. | The Psychology of Computer Cooking | MC3026 | null |
  18. | But Is It User Friendly? | PC1035 | 22.9500 |
  19. | Secrets of Silicon Valley | PC8888 | 20.0000 |
  20. | Net Etiquette | PC9999 | null |
  21. | Computer Phobic AND Non-Phobic Individuals: Behavior Variations | PS1372 | 21.5900 |
  22. | Is Anger the Enemy? | PS2091 | 10.9500 |
  23. | Life Without Fear | PS2106 | 7.0000 |
  24. | Prolonged Data Deprivation: Four Case Studies | PS3333 | 19.9900 |
  25. | Emotional Security: A New Algorithm | PS7777 | 7.9900 |
  26. | Onions | TC3218 | trad_cook |
  27. | Fifty Years in Buckingham Palace Kitchens | TC4203 | 11.9500 |
  28. | Sushi | TC7777 | 0877 |
  29. +------------------------------------------------------------------+-----------+---------------+
  30. 16 rows selected (1.121 seconds)

Like通配符

Mysql:

  1. mysql> select title,title_id,price from titles where title like 'You %';
  2. +---------------------------------+----------+--------+
  3. | title | title_id | price |
  4. +---------------------------------+----------+--------+
  5. | You Can Combat Computer Stress! | BU2075 | 2.9900 |
  6. +---------------------------------+----------+--------+
  7. 1 row in set (0.01 sec)
  8. mysql> select title,title_id,price from titles where title like 'I_ %';
  9. +---------------------+----------+---------+
  10. | title | title_id | price |
  11. +---------------------+----------+---------+
  12. | Is Anger the Enemy? | PS2091 | 10.9500 |
  13. +---------------------+----------+---------+
  14. 1 row in set (0.00 sec)

Inceptor:

  1. 0: jdbc:hive2://192.168.1.70:10000/> select title,title_id,price from titles where title like 'You %';
  2. +----------------------------------+-----------+---------+
  3. | title | title_id | price |
  4. +----------------------------------+-----------+---------+
  5. | You Can Combat Computer Stress! | BU2075 | 2.9900 |
  6. +----------------------------------+-----------+---------+
  7. 1 row selected (1.057 seconds)
  8. 0: jdbc:hive2://192.168.1.70:10000/> select title,title_id,price from titles where title like 'I_ %';
  9. +----------------------+-----------+----------+
  10. | title | title_id | price |
  11. +----------------------+-----------+----------+
  12. | Is Anger the Enemy? | PS2091 | 10.9500 |
  13. +----------------------+-----------+----------+
  14. 1 row selected (1.472 seconds)

使用数据处理函数

函数没有SQL的可移值性强,能运行在多个系统上的代码称为可移值的,相对来说,多数SQL语句是可移植的,在SQL实现上有一定的差异,这些差异通常不那么难处理.而函数的可移值性却不强.几乎每种主要的DBMS的实现都支持其他实现不支持的函数,而且有时差异还很大.

为了代码的可移植性,请保证做好代码的注释,以便以后你能确切地知道所编写SQL代码的含义.

MYSQL:

  1. mysql> select upper(title) from titles;
  2. +-----------------------------------------------------------------+
  3. | upper(title) |
  4. +-----------------------------------------------------------------+
  5. | BUT IS IT USER FRIENDLY? |
  6. | COMPUTER PHOBIC AND NON-PHOBIC INDIVIDUALS: BEHAVIOR VARIATIONS |
  7. | COOKING WITH COMPUTERS: SURREPTITIOUS BALANCE SHEETS |
  8. | EMOTIONAL SECURITY: A NEW ALGORITHM |
  9. | FIFTY YEARS IN BUCKINGHAM PALACE KITCHENS |
  10. | IS ANGER THE ENEMY? |
  11. | LIFE WITHOUT FEAR |
  12. | NET ETIQUETTE |
  13. | ONIONS, LEEKS, AND GARLIC: COOKING SECRETS OF THE MEDITERRANEAN |
  14. | PROLONGED DATA DEPRIVATION: FOUR CASE STUDIES |
  15. | SECRETS OF SILICON VALLEY |
  16. | SILICON VALLEY GASTRONOMIC TREATS |
  17. | STRAIGHT TALK ABOUT COMPUTERS |
  18. | SUSHI, ANYONE? |
  19. | THE BUSY EXECUTIVE'S DATABASE GUIDE |
  20. | THE GOURMET MICROWAVE |
  21. | THE PSYCHOLOGY OF COMPUTER COOKING |
  22. | YOU CAN COMBAT COMPUTER STRESS! |
  23. +-----------------------------------------------------------------+
  24. 18 rows in set (0.00 sec)

Inceptor:

见TDH官方手册上的函数支持

聚集函数

MYSQL:

  1. mysql> select avg(price) from titles;
  2. +-------------+
  3. | avg(price) |
  4. +-------------+
  5. | 14.76625000 |
  6. +-------------+
  7. 1 row in set (0.00 sec)
  8. mysql> select count(price) from titles;
  9. +--------------+
  10. | count(price) |
  11. +--------------+
  12. | 16 |
  13. +--------------+
  14. 1 row in set (0.00 sec)
  15. mysql> select max(price) from titles;
  16. +------------+
  17. | max(price) |
  18. +------------+
  19. | 22.9500 |
  20. +------------+
  21. 1 row in set (0.00 sec)
  22. mysql> select min(price) from titles;
  23. +------------+
  24. | min(price) |
  25. +------------+
  26. | 2.9900 |
  27. +------------+
  28. 1 row in set (0.00 sec)
  29. mysql> select sum(price) from titles;
  30. +------------+
  31. | sum(price) |
  32. +------------+
  33. | 236.2600 |
  34. +------------+
  35. 1 row in set (0.00 sec)
  36. mysql> select avg( distinct price) from titles;
  37. +----------------------+
  38. | avg( distinct price) |
  39. +----------------------+
  40. | 14.66818182 |
  41. +----------------------+
  42. 1 row in set (0.03 sec)
  43. mysql> select count(*) as title_items,
  44. -> min(price) as price_min,
  45. -> max(price) as price_max,
  46. -> avg(price) as price_avg
  47. -> from titles;
  48. +-------------+-----------+-----------+-------------+
  49. | title_items | price_min | price_max | price_avg |
  50. +-------------+-----------+-----------+-------------+
  51. | 18 | 2.9900 | 22.9500 | 14.76625000 |
  52. +-------------+-----------+-----------+-------------+
  53. 1 row in set (0.00 sec)

Inceptor:

  1. 0: jdbc:hive2://192.168.1.70:10000/> select avg(price) as price_avg from titles;
  2. +--------------------+
  3. | price_avg |
  4. +--------------------+
  5. | 71.82133333333334 |
  6. +--------------------+
  7. 1 row selected (1.033 seconds)
  8. 0: jdbc:hive2://192.168.1.70:10000/> select count(price) from titles;
  9. +------+
  10. | _c0 |
  11. +------+
  12. | 18 |
  13. +------+
  14. 1 row selected (1.339 seconds)
  15. 0: jdbc:hive2://192.168.1.70:10000/> select avg( distinct price) from titles;
  16. +---------------------+
  17. | _c0 |
  18. +---------------------+
  19. | 100.24100000000001 |
  20. +---------------------+
  21. 0: jdbc:hive2://192.168.1.70:10000/> select count(*) as title_items,min(price) as price_min,max(price) as price_max,avg(price) as price_avg from titles;
  22. +--------------+------------+---------------+--------------------+
  23. | title_items | price_min | price_max | price_avg |
  24. +--------------+------------+---------------+--------------------+
  25. | 18 | 0877 | trad_cook | 71.82133333333334 |
  26. +--------------+------------+---------------+--------------------+
  27. 1 row selected (0.899 seconds)

数据分组

Mysql:

  1. mysql> select type from titles group by type;
  2. +--------------+
  3. | type |
  4. +--------------+
  5. | business |
  6. | mod_cook |
  7. | popular_comp |
  8. | psychology |
  9. | trad_cook |
  10. | UNDECIDED |
  11. +--------------+
  12. 6 rows in set (0.01 sec)
  13. mysql> select type,avg(price)as book_avg_price from titles group by type having book_avg_price>10;
  14. +--------------+----------------+
  15. | type | book_avg_price |
  16. +--------------+----------------+
  17. | business | 13.73000000 |
  18. | mod_cook | 11.49000000 |
  19. | popular_comp | 21.47500000 |
  20. | psychology | 13.50400000 |
  21. | trad_cook | 15.96333333 |
  22. +--------------+----------------+
  23. 5 rows in set (0.00 sec)
  24. mysql> select title_id,sum(qty) as book_sum_qty from sales where title_id in (select title_id from titles where title='Sushi, Anyone?');
  25. +----------+--------------+
  26. | title_id | book_sum_qty |
  27. +----------+--------------+
  28. | TC7777 | 20 |
  29. +----------+--------------+
  30. 1 row in set (0.00 sec)

Inceptor:

  1. 0: jdbc:hive2://192.168.1.70:10000/> select type from titles group by type;
  2. +---------------+
  3. | type |
  4. +---------------+
  5. | psychology |
  6. | popular_comp |
  7. | trad_cook |
  8. | business |
  9. | Leeks |
  10. | UNDECIDED |
  11. | mod_cook |
  12. | Anyone? |
  13. +---------------+
  14. 0: jdbc:hive2://192.168.1.70:10000/> select type,avg(price)as book_avg_price from titles group by type having book_avg_price>10;
  15. +---------------+---------------------+
  16. | type | book_avg_price |
  17. +---------------+---------------------+
  18. | psychology | 13.504 |
  19. | popular_comp | 21.475 |
  20. | trad_cook | 11.95 |
  21. | business | 13.73 |
  22. | mod_cook | 11.489999999999998 |
  23. | Anyone? | 877.0 |
  24. +---------------+---------------------+
  25. 6 rows selected (2.829 seconds)
  26. select s.title_id,sum(s.qty) as book_sum_qty from sales as s where s.title_id in (select t.title_id from titles t where t.title='Sushi, Anyone?') group by s.title_id;
  27. +-----------+---------------+
  28. | title_id | book_sum_qty |
  29. +-----------+---------------+
  30. +-----------+---------------+
  31. No rows selected (2.285 seconds)

表连接

MYSQL:

  1. mysql> desc titles;
  2. +-----------+---------------+------+-----+-----------+-------+
  3. | Field | Type | Null | Key | Default | Extra |
  4. +-----------+---------------+------+-----+-----------+-------+
  5. | title_id | varchar(6) | NO | PRI | NULL | |
  6. | title | varchar(80) | NO | MUL | NULL | |
  7. | type | varchar(12) | NO | | UNDECIDED | |
  8. | pub_id | varchar(4) | YES | | NULL | |
  9. | price | decimal(19,4) | YES | | NULL | |
  10. | advance | decimal(19,4) | YES | | NULL | |
  11. | royalty | int(11) | YES | | NULL | |
  12. | ytd_sales | int(11) | YES | | NULL | |
  13. | notes | varchar(200) | YES | | NULL | |
  14. | pubdate | datetime | NO | | NULL | |
  15. +-----------+---------------+------+-----+-----------+-------+
  16. 10 rows in set (0.00 sec)
  17. mysql> desc titleauthor;
  18. +------------+---------------------+------+-----+---------+-------+
  19. | Field | Type | Null | Key | Default | Extra |
  20. +------------+---------------------+------+-----+---------+-------+
  21. | au_id | varchar(11) | NO | PRI | NULL | |
  22. | title_id | varchar(6) | NO | PRI | NULL | |
  23. | au_ord | tinyint(3) unsigned | YES | | NULL | |
  24. | royaltyper | int(11) | YES | | NULL | |
  25. +------------+---------------------+------+-----+---------+-------+
  26. 4 rows in set (0.00 sec)
  27. mysql> desc authors;
  28. +----------+-------------+------+-----+---------+-------+
  29. | Field | Type | Null | Key | Default | Extra |
  30. +----------+-------------+------+-----+---------+-------+
  31. | au_id | varchar(11) | NO | PRI | NULL | |
  32. | au_lname | varchar(40) | NO | MUL | NULL | |
  33. | au_fname | varchar(20) | NO | | NULL | |
  34. | phone | varchar(12) | NO | | UNKNOWN | |
  35. | address | varchar(40) | YES | | NULL | |
  36. | city | varchar(20) | YES | | NULL | |
  37. | state | varchar(2) | YES | | NULL | |
  38. | zip | varchar(5) | YES | | NULL | |
  39. | contract | bit(1) | NO | | NULL | |
  40. +----------+-------------+------+-----+---------+-------+
  41. 9 rows in set (0.00 sec)
  42. mysql> select title,au_ord from titles inner join titleauthor on titles.title_id=titleauthor.title_id;
  43. +-----------------------------------------------------------------+--------+
  44. | title | au_ord |
  45. +-----------------------------------------------------------------+--------+
  46. | But Is It User Friendly? | 1 |
  47. | Computer Phobic AND Non-Phobic Individuals: Behavior Variations | 2 |
  48. | Computer Phobic AND Non-Phobic Individuals: Behavior Variations | 1 |
  49. | Cooking with Computers: Surreptitious Balance Sheets | 2 |
  50. | Cooking with Computers: Surreptitious Balance Sheets | 1 |
  51. | Emotional Security: A New Algorithm | 1 |
  52. | Fifty Years in Buckingham Palace Kitchens | 1 |
  53. | Is Anger the Enemy? | 2 |
  54. | Is Anger the Enemy? | 1 |
  55. | Life Without Fear | 1 |
  56. | Net Etiquette | 1 |
  57. | Onions, Leeks, and Garlic: Cooking Secrets of the Mediterranean | 1 |
  58. | Prolonged Data Deprivation: Four Case Studies | 1 |
  59. | Secrets of Silicon Valley | 1 |
  60. | Secrets of Silicon Valley | 2 |
  61. | Silicon Valley Gastronomic Treats | 1 |
  62. | Straight Talk About Computers | 1 |
  63. | Sushi, Anyone? | 2 |
  64. | Sushi, Anyone? | 3 |
  65. | Sushi, Anyone? | 1 |
  66. | The Busy Executive's Database Guide | 2 |
  67. | The Busy Executive's Database Guide | 1 |
  68. | The Gourmet Microwave | 1 |
  69. | The Gourmet Microwave | 2 |
  70. | You Can Combat Computer Stress! | 1 |
  71. +-----------------------------------------------------------------+--------+
  72. 25 rows in set (0.00 sec)
  73. mysql> select title,au_ord from titles inner join titleauthor on titles.title_id=titleauthor.title_id inner join authors on authors.au_id=titleauthor.au_id;
  74. +-----------------------------------------------------------------+--------+
  75. | title | au_ord |
  76. +-----------------------------------------------------------------+--------+
  77. | But Is It User Friendly? | 1 |
  78. | Computer Phobic AND Non-Phobic Individuals: Behavior Variations | 2 |
  79. | Computer Phobic AND Non-Phobic Individuals: Behavior Variations | 1 |
  80. | Cooking with Computers: Surreptitious Balance Sheets | 2 |
  81. | Cooking with Computers: Surreptitious Balance Sheets | 1 |
  82. | Emotional Security: A New Algorithm | 1 |
  83. | Fifty Years in Buckingham Palace Kitchens | 1 |
  84. | Is Anger the Enemy? | 2 |
  85. | Is Anger the Enemy? | 1 |
  86. | Life Without Fear | 1 |
  87. | Net Etiquette | 1 |
  88. | Onions, Leeks, and Garlic: Cooking Secrets of the Mediterranean | 1 |
  89. | Prolonged Data Deprivation: Four Case Studies | 1 |
  90. | Secrets of Silicon Valley | 1 |
  91. | Secrets of Silicon Valley | 2 |
  92. | Silicon Valley Gastronomic Treats | 1 |
  93. | Straight Talk About Computers | 1 |
  94. | Sushi, Anyone? | 2 |
  95. | Sushi, Anyone? | 3 |
  96. | Sushi, Anyone? | 1 |
  97. | The Busy Executive's Database Guide | 2 |
  98. | The Busy Executive's Database Guide | 1 |
  99. | The Gourmet Microwave | 1 |
  100. | The Gourmet Microwave | 2 |
  101. | You Can Combat Computer Stress! | 1 |
  102. +-----------------------------------------------------------------+--------+
  103. 25 rows in set (0.00 sec)
  104. mysql> select title,au_ord from titles left join titleauthor on titles.title_id=titleauthor.title_id;
  105. +-----------------------------------------------------------------+--------+
  106. | title | au_ord |
  107. +-----------------------------------------------------------------+--------+
  108. | But Is It User Friendly? | 1 |
  109. | Computer Phobic AND Non-Phobic Individuals: Behavior Variations | 2 |
  110. | Computer Phobic AND Non-Phobic Individuals: Behavior Variations | 1 |
  111. | Cooking with Computers: Surreptitious Balance Sheets | 2 |
  112. | Cooking with Computers: Surreptitious Balance Sheets | 1 |
  113. | Emotional Security: A New Algorithm | 1 |
  114. | Fifty Years in Buckingham Palace Kitchens | 1 |
  115. | Is Anger the Enemy? | 2 |
  116. | Is Anger the Enemy? | 1 |
  117. | Life Without Fear | 1 |
  118. | Net Etiquette | 1 |
  119. | Onions, Leeks, and Garlic: Cooking Secrets of the Mediterranean | 1 |
  120. | Prolonged Data Deprivation: Four Case Studies | 1 |
  121. | Secrets of Silicon Valley | 1 |
  122. | Secrets of Silicon Valley | 2 |
  123. | Silicon Valley Gastronomic Treats | 1 |
  124. | Straight Talk About Computers | 1 |
  125. | Sushi, Anyone? | 2 |
  126. | Sushi, Anyone? | 3 |
  127. | Sushi, Anyone? | 1 |
  128. | The Busy Executive's Database Guide | 2 |
  129. | The Busy Executive's Database Guide | 1 |
  130. | The Gourmet Microwave | 1 |
  131. | The Gourmet Microwave | 2 |
  132. | The Psychology of Computer Cooking | NULL |
  133. | You Can Combat Computer Stress! | 1 |
  134. +-----------------------------------------------------------------+--------+
  135. 26 rows in set (0.00 sec)
  136. mysql> select title,au_ord from titles right join titleauthor on titles.title_id=titleauthor.title_id;
  137. +-----------------------------------------------------------------+--------+
  138. | title | au_ord |
  139. +-----------------------------------------------------------------+--------+
  140. | Prolonged Data Deprivation: Four Case Studies | 1 |
  141. | The Busy Executive's Database Guide | 2 |
  142. | You Can Combat Computer Stress! | 1 |
  143. | But Is It User Friendly? | 1 |
  144. | Cooking with Computers: Surreptitious Balance Sheets | 2 |
  145. | Sushi, Anyone? | 2 |
  146. | Straight Talk About Computers | 1 |
  147. | The Busy Executive's Database Guide | 1 |
  148. | Secrets of Silicon Valley | 1 |
  149. | Sushi, Anyone? | 3 |
  150. | Net Etiquette | 1 |
  151. | Emotional Security: A New Algorithm | 1 |
  152. | Fifty Years in Buckingham Palace Kitchens | 1 |
  153. | Sushi, Anyone? | 1 |
  154. | Silicon Valley Gastronomic Treats | 1 |
  155. | The Gourmet Microwave | 1 |
  156. | Cooking with Computers: Surreptitious Balance Sheets | 1 |
  157. | Computer Phobic AND Non-Phobic Individuals: Behavior Variations | 2 |
  158. | Computer Phobic AND Non-Phobic Individuals: Behavior Variations | 1 |
  159. | Onions, Leeks, and Garlic: Cooking Secrets of the Mediterranean | 1 |
  160. | Secrets of Silicon Valley | 2 |
  161. | The Gourmet Microwave | 2 |
  162. | Is Anger the Enemy? | 2 |
  163. | Is Anger the Enemy? | 1 |
  164. | Life Without Fear | 1 |
  165. +-----------------------------------------------------------------+--------+
  166. 25 rows in set (0.00 sec)

Inceptor:

  1. 0: jdbc:hive2://192.168.1.70:10000/> select title,au_ord from titles inner join titleauthor on titles.title_id=titleauthor.title_id;
  2. +------------------------------------------------------------------+---------+
  3. | title | au_ord |
  4. +------------------------------------------------------------------+---------+
  5. | The Busy Executive's Database Guide | 2 |
  6. | The Busy Executive's Database Guide | 1 |
  7. | Cooking with Computers: Surreptitious Balance Sheets | 2 |
  8. | Cooking with Computers: Surreptitious Balance Sheets | 1 |
  9. | You Can Combat Computer Stress! | 1 |
  10. | Straight Talk About Computers | 1 |
  11. | Silicon Valley Gastronomic Treats | 1 |
  12. | The Gourmet Microwave | 1 |
  13. | The Gourmet Microwave | 2 |
  14. | But Is It User Friendly? | 1 |
  15. | Secrets of Silicon Valley | 1 |
  16. | Secrets of Silicon Valley | 2 |
  17. | Net Etiquette | 1 |
  18. | Computer Phobic AND Non-Phobic Individuals: Behavior Variations | 2 |
  19. | Computer Phobic AND Non-Phobic Individuals: Behavior Variations | 1 |
  20. | Is Anger the Enemy? | 2 |
  21. | Is Anger the Enemy? | 1 |
  22. | Life Without Fear | 1 |
  23. | Prolonged Data Deprivation: Four Case Studies | 1 |
  24. | Emotional Security: A New Algorithm | 1 |
  25. | Onions | 1 |
  26. | Fifty Years in Buckingham Palace Kitchens | 1 |
  27. | Sushi | 2 |
  28. | Sushi | 3 |
  29. | Sushi | 1 |
  30. +------------------------------------------------------------------+---------+
  31. 25 rows selected (1.858 seconds)