【问题标题】:Grep on a joined table MySQL/Linux连接表 MySQL/Linux 上的 Grep
【发布时间】:2011-05-01 20:10:57
【问题描述】:

我似乎无法在这张桌子上得到 grep -v "Africa" 而不会抛出错误,有人可以帮助我在 Ubuntu 中使用语法吗?

某处出现错误,我的一个国家/地区没有定义宏观区域(即美洲、非洲、亚洲、欧洲),我找不到它。我需要对这个连接表进行 grep 以查看哪个国家/地区只有子区域。

select country, group_concat(region) as regions from regions group by country;

(其页脚如下所示)

| Ukraine                   | Eastern Europe,Europe
| United Arab Emirates      | Middle East,Persian Gulf,Western Asia
| United Kingdom            | Europe
| United States             | Americas,North America
| Uruguay                   | Americas,South America
| Uzbekistan                | Asia,Central Asia
| Vanuatu                   | Australasia,Melanesia,Oceania
| Venezuela                 | Amazon,Americas,Andes,South America
| Vietnam                   | Asia,East Asia,Indochina
| Virgin Islands            | Americas,Caribbean
| Wallis and Futuna Islands | Australasia,Oceania,Polynesia
| Yemen                     | Middle East,Persian Gulf,Western Asia
| Zambia                    | Africa,Central Africa,East Africa,Southern Africa
| Zimbabwe                  | Africa,Central Africa,East Africa,Southern Africa  

我使用:

mysql -u root -pPASS "使用报告"| mysql -u root -pPASS -e "select country, group_concat(region) as region from regions group by country;"| grep -v '非洲'

我得到了错误:

ERROR 1049 (42000): Unknown database 'use reports' ERROR 1046 (3D000) at line 1: 未选择数据库

【问题讨论】:

  • 您如何执行 grep(在保存的文本文件上?在直接来自 MySQL 的管道上?等等),您遇到了什么样的错误?
  • mysql -u root -pPASS "使用报告"| mysql -u root -pPASS -e "select country, group_concat(region) as region from regions group by country;"| grep -v 'Africa' ERROR 1049 (42000): Unknown database 'use reports' ERROR 1046 (3D000) at line 1: 未选择数据库
  • ryandward@RDW:~$ mysql -u root -pPass -e "使用教程;按国家/地区分组选择国家,group_concat(region) 作为地区;"| grep -v "欧洲" | grep -v "亚洲" | grep -v "非洲" | grep -v "美洲" | grep -v "Australasia" country region 阿根廷 安第斯山脉 菲律宾 南中国海 这真是一件可怕的事情,哈哈,但它奏效了!
  • 啊,你明白了。 :-) 干得好。
  • @Ryan,请回答您自己的问题并将解决方案不是放在评论中,而是放在答案中。一段时间后(24 小时 IIRC),您可以接受该答案。这样其他人如何有一个类似的问题可以看到你的问题,看到它已被成功回答并从你的错误中学习^H^H经验。

标签: mysql linux ubuntu grep mysql-error-1049


【解决方案1】:
mysql -u root -pPASS reports -e "select country, group_concat(region) as regions from regions group by country;" | grep -v 'Africa'

第一件事:您选择了错误的数据库。数据库名称是一个位置参数;不需要做内嵌的sql语句。

其次,您不能在管道中使用mysql 命令并让第一个命令影响第二个,因为在执行第二个语句之前数据库连接已关闭;它们是完全不同的联系。如果您需要多个查询,请在语句中使用分号。上述命令的另一种形式如下所示:

mysql -u root -pPASS -e "use reports; select country, group_concat(region) as regions from regions group by country;" | grep -v 'Africa'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-23
    相关资源
    最近更新 更多