简体   繁体   English

使用MySQL命令行,如何在Select语句中仅显示日期或金额(十进制)列?

[英]Using the MySQL command line, how can I display only date or amount (Decimal) columns in a Select statement?

I am new to SQL and I realize I have a lot to learn. 我是SQL的新手,我意识到我有很多需要学习的东西。 I am in the midst of converting a set of ISAM files into MySQL tables. 我正在将一组ISAM文件转换为MySQL表。 Some of the tables I am creating have hundreds of columns. 我创建的一些表有数百列。 To make it easier to see if my conversions are working properly, I would like to display only a subset of columns at a time in my SELECT results. 为了更容易查看我的转换是否正常工作,我想在SELECT结果中一次只显示一列列。

For example, I would like to see only Dates or only Amounts (ie, Decimals). 例如,我想只看到Dates或Amounts(即Decimals)。 This would make it easier to see if correct values are in those fields. 这样可以更容易地查看这些字段中是否存在正确的值。

I am working from the MySQL command line and I would rather not specify a long list of column names if it can be avoided. 我正在从MySQL命令行工作,如果可以避免,我宁愿不指定一长列列名。 Is there a way to get my Select statements to display only the types of columns I am interested in? 有没有办法让我的Select语句只显示我感兴趣的列类型?


(Added 5/12/16) I used a variant of Uueerdo's syntax to create a view: (已添加5/12/16)我使用了Uueerdo语法的变体来创建视图:

create view dates_view as concat('select ', group_concat(column_name), ' ',' from ', table_schema, '.', table_name, ';') from information_schema.columns where table_name = 'binders_tbl' and data_type in ('date'); 创建视图dates_view为concat('select',group_concat(column_name),'','from',table_schema,'。',table_name,';')来自information_schema.columns,其中table_name ='binders_tbl'和data_type in('date “);

When I enter select * from dates_view; 当我从dates_view输入select *时; I get this value: 我得到这个值:

select binder_effectiveDate,binder_expirationDate,binder_submissionCreatedDate,binder_f‌​irstQuotedDate,binder_boundDate,binder_invoicedDate,binder_terminatedDate,binder_‌​cancelledDate from aesdb.binders_tbl; 从aesdb.binders_tbl中选择binder_effectiveDate,binder_expirationDate,binder_submissionCreatedDate,binder_f irstQuotedDate,binder_boundDate,binder_invoicedDate,binder_terminatedDate,binder_ cancelledDate;

I tried but could not find a way to use this view with "select * from binders_tbl;" 我试过但是找不到使用“select * from binders_tbl”这个视图的方法。 to display only the above date fields and their values. 仅显示上述日期字段及其值。

If anyone can guide me to a "join" or "subquery" solution, I would appreciate it. 如果有人可以引导我进入“加入”或“子查询”解决方案,我将不胜感激。 For now, I am going to stick with listing all the individual columns as needed. 现在,我将坚持根据需要列出所有单独的列。 Plus, I will study more basic and intermediate SQL. 另外,我将学习更多基础和中级SQL。 Thank you to those who responded. 谢谢那些回复的人。


(Added 5/20/16) I found a sort-of work-around to my problem . (已添加5/20/16) 我找到了解决问题的方法 Here it is in case it helps someone else: 这是为了以防万一:

CREATE VIEW DateCols_View AS SELECT Table1_SelectionCriteriaField, Table1_Date1, Table1_Date2, Table1_Date3 FROM Table1; CREATE VIEW DateCols_View AS SELECT Table1_SelectionCriteriaField,Table1_Date1,Table1_Date2,Table1_Date3 FROM Table1;

SELECT * FROM DateCols_View WHERE Table1_SelectionCriteriaField (matches some criteria); SELECT * FROM DateCols_View WHERE Table1_SelectionCriteriaField(匹配某些条件);

My work-around has 2 drawbacks . 我的解决方法有两个缺点 The first is that I have to include any selection criteria fields that I plan to use later. 首先 ,我必须包括我计划稍后使用的任何选择标准字段。 But with a little forethought I can create multiple views with different sets of columns for different displays. 但是稍微考虑一下,我可以为不同的显示器创建具有不同列的多个视图。 This will let me display only those columns I wish to see while working with a set of records. 这将让我只显示我希望在处理一组记录时看到的列。 I plan to create separate views for Dates, Amounts, Flags, etc. 我打算为Dates,Amounts,Flags等创建单独的视图。

The other drawback is that I have to create a separate set of views for each table or join combination I plan to work with. 另一个缺点是我必须为我计划使用的每个表或联接组合创建一组单独的视图。

I hope as I learn more about SQL, I will find a way to generalize this technique (to make it more like a script or macro) and simplify it further. 我希望随着我对SQL的了解越来越多,我将找到一种方法来推广这种技术(使其更像脚本或宏)并进一步简化它。

Nope, you have to specify column names. 不,你必须指定列名。 There is no syntax for dynamic field inclusion, however you could maybe work something out with information_schema (like below) to generate a query for you to use. 动态字段包含没有语法,但您可以使用information_schema(如下所示)来生成查询供您使用。

SELECT CONCAT('SELECT ', GROUP_CONCAT(column_name), ' '
,'FROM `', table_schema, '`.`', table_name, '`;'
FROM information_schema.columns 
WHERE table_schema = 'yourschemaname' 
AND table_name = 'yourtablename' 
AND data_type IN ('timestamp', 'date'[, etc....])
;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用命令行向用户mysql只授予选择行的权限? - how to grant select only rows to user mysql using command line? Mysql基于多个列选择唯一记录,并且仅显示组和总金额 - Mysql Select unique record based on multiple columns and display only group and sum amount MySQL - 如何使用select语句插入多行? - MySQL - How can I insert multiple rows using select statement? 我可以从 mysql 命令行运行 IF 语句吗? v 5.6 - Can I run an IF statement form the mysql command line? v 5.6 如何从带有日期和金额列的表格中选择峰值金额日期 - How to select peak amount dates from table with date and amount columns 如何选择一个不同的值,然后按顺序使用mysql和PHP返回值? - How can I select a Distinct value and then order by amount returned using mysql and PHP? 如何在不希望字符串中第二个字母的情况下为mysql写一条select语句? - How can I write a select statement for mysql where I only do not want the second letter in the string? 如何选择数据库中类型为Decimal(10,0)的每个表的所有列,以便在MySQL中更改为Decimal(10,2) - How can I Select all Columns of every Table in my Database whose type is of Decimal(10,0) so that i need to change to Decimal(10,2) in MySQL 如何使用mysql命令条件语句在Crystal报表中显示数据? - how to display data in crystal reports using mysql command conditional statement? 我可以对SELECT语句对MySql另一个表上子对象的数量进行排序吗? - Can I sort a SELECT statement on the amount of child objects on another table in MySql?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM