{"id":649,"date":"2010-03-05T13:35:40","date_gmt":"2010-03-05T13:35:40","guid":{"rendered":"http:\/\/kedar.nitty-witty.com\/?p=649"},"modified":"2023-05-14T10:23:18","modified_gmt":"2023-05-14T10:23:18","slug":"ideas-for-select-all-columns-but-one-mysql-stored-procedure","status":"publish","type":"post","link":"https:\/\/kedar.nitty-witty.com\/blog\/ideas-for-select-all-columns-but-one-mysql-stored-procedure","title":{"rendered":"Ideas for select all columns but one mysql stored procedure"},"content":{"rendered":"\n<p>Assume we&#8217;ve a table with 100 rows and we need to select all columns but one.<br>The problem is headache of actually typing out all 99 required columns!! In this blog we will see how can we select all columns but one from the table.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Solutions \/ Ideas to above problem are:<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Ignorance is bliss. Select all(*) and ignore the column.<\/li>\n\n\n\n<li>Manually type column names or manage it with copy paste!<\/li>\n\n\n\n<li>Create a view from original table and drop the unnecessary column!<\/li>\n\n\n\n<li>MySQL Database command \/ syntax that allows us to do this; which is not there unfortunately.<\/li>\n\n\n\n<li>Use my stored procedure to select all but one columns:<\/li>\n<\/ol>\n\n\n\n<p>No need to explain why all such hacks are initiated with the help of information_schema!! \ud83d\ude42<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">The idea:<\/h3>\n\n\n\n<p>Prepare column list to be selected from COLUMNS table of information_schema database for specified database name and table name. Thus we can select all columns but one and prepare sql statement and execute.<br>It can be further extended for select all but SOME; instead of &#8220;column_name&lt;&gt;in_colm_nm&#8221; one can use &#8220;column_name not in &#8230;&#8221; to prepare the column list to be selected.<\/p>\n\n\n\n<p>Here you go with the code: [Download selectAllButOne.sql\u00a0at the end of the page.]<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>DELIMITER $$\n<em>DROP PROCEDURE IF EXISTS `selectAllButOne` $$\nCREATE DEFINER=`root`@`localhost` PROCEDURE `selectAllButOne`(in_db_nm varchar(20),in_tbl_nm varchar(20),in_colm_nm varchar(20))\nBEGIN<\/em>\nSET @stmnt= CONCAT('SELECT ',(SELECT GROUP_CONCAT(column_name) FROM information_schema.columns WHERE table_name=in_tbl_nm AND table_schema=in_db_nm AND column_name&lt;>in_colm_nm), ' FROM ',in_db_nm,'.',in_tbl_nm,';');\nPREPARE stmnt FROM @stmnt;\nEXECUTE stmnt;\nEND $$\nDELIMITER ;<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>As one of the ideas, #4 says about MySQL syntax come to our rescue.<\/p>\n\n\n\n<p>I wish if MySQL can ease out us with syntax as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT * FROM TABLENAME EXCEPT COLUMN COLUMNNAME&#91;,COLUMNNAME...]<\/code><\/pre>\n\n\n\n<p>But until then we have this quick work around<\/p>\n\n\n\n<p><b>Download <a href=\"http:\/\/kedar.nitty-witty.com\/wp-content\/uploads\/2010\/03\/selectallbutone.sql_.txt\" target=\"_blank\" rel=\"noopener\">Select all but one mysql stored procedure.<\/a><br><\/b><br>Any other ideas?<\/p>\n","protected":false},"excerpt":{"rendered":"Assume we&#8217;ve a table with 100 rows and we need to select all columns but one.The problem is headache of actually typing out all 99 required columns!! In this blog&hellip;\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[8,378],"tags":[52,427,587,588,104,108],"class_list":{"0":"post-649","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-mysql","7":"category-mysql-scripts-mysql","8":"tag-ideas","9":"tag-mysql","10":"tag-mysql-select-columns","11":"tag-procedure-to-select-all-but-one","12":"tag-select-all-but-one","13":"tag-stored-procedure"},"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/posts\/649","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/comments?post=649"}],"version-history":[{"count":5,"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/posts\/649\/revisions"}],"predecessor-version":[{"id":2893,"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/posts\/649\/revisions\/2893"}],"wp:attachment":[{"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/media?parent=649"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/categories?post=649"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/tags?post=649"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}