MySQL/Linux 连接表上的 Grep

发布于 2024-11-04 09:33:43 字数 1395 浏览 3 评论 0原文

我似乎无法在这个表上得到 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“选择国家/地区,group_concat(区域)作为按国家/地区分组的区域;”| grep -v '非洲'

我收到错误:

错误 1049 (42000):未知数据库“使用报告”第 1 行错误 1046 (3D000):未选择数据库

I can't seem to get grep -v "Africa" on this table without throwing errors, can someone help me with syntax in Ubuntu?

There is an error somewhere, and one of my countries doesn't have a macroregion defined (i.e. Americas, Africa, Asia, Europe), and I can't find it. I need to grep this joined table to see which country only has sub regions.

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

(footer of which looks like this)

| 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  

I use:

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

And I get the error:

ERROR 1049 (42000): Unknown database 'use reports' ERROR 1046 (3D000) at line 1: No database selected

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

葬シ愛 2024-11-11 09:33:53
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'
mysql -u root -pPASS reports -e "select country, group_concat(region) as regions from regions group by country;" | grep -v 'Africa'

first things first: you selected your database wrong. The database name is a positional parameter; you don't need to do an embedded sql statement.

secondly, you can't use the mysql command in a pipeline and have the first command affect the second, since the database connection is closed prior to the second statement being executed; they're entirely different connections. Use a semicolon inside the statement if you need multiple queries. An alternative form of the aforementioned command that does so would look like this:

mysql -u root -pPASS -e "use reports; select country, group_concat(region) as regions from regions group by country;" | grep -v 'Africa'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文