如何在mysql查询中添加更多列

时间:2012-03-27 00:32:57

标签: mysql sql select

我有这个SQL查询:

SELECT DISTINCT tb.SERIAL_NUM, tb.REVISION_NUM, tb.JOB_NUM, tb.JOB_TOTAL, tb.TEST_PROC, tb.BOX_NUM, tb.TEST_BENCH, tb.OPERATOR, tb.SW_REVISION, tb.TEST_DATE, tb.VISUAL_INSP, tb.TEST, tb.STATUS FROM tb
WHERE tb.SERIAL_NUM>=%s  
AND tb.SERIAL_NUM <=%s 
AND tb.TEST = 'Pass' GROUP BY  tb.SERIAL_NUM

它工作正常,但在我的表中,我想在此查询中添加200多列,例如:

SELECT DISTINCT tb.SERIAL_NUM, tb.REVISION_NUM, tb.JOB_NUM, tb.JOB_TOTAL, tb.TEST_PROC, tb.BOX_NUM, tb.TEST_BENCH, tb.OPERATOR, tb.SW_REVISION, tb.TEST_DATE, tb.VISUAL_INSP, tb.TEST, tb.STATUS, column15.....,column215 FROM tb
WHERE tb.SERIAL_NUM>=%s  AND tb.SERIAL_NUM <=%s AND tb.TEST = 'Pass' GROUP BY  tb.SERIAL_NUM

是否有一种简单的方法可以在查询中添加这些列?

2 个答案:

答案 0 :(得分:2)

为什么不使用select * like this

SELECT DISTINCT * FROM <table> WHERE <whereClause>

答案 1 :(得分:1)

这是获得所有领域的不同方式。然后复制并粘贴到您的查询中...架构/系统表确实很强大。

SELECT Group_concat(COLUMN_NAME Separator ', ') as allTableFields
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_name = 'tb'
Group By table_name

这样做(或应该做什么)是从表tb的数据库中的列中获取所有字段的列表。然后它用逗号分隔它们,然后用空格作为分隔符。所以结果应该是

field1,field2,field3,field4,field5,field6 ... field215