Without formally scripting a Stored Procedure, this is as good as it is going to get. The SELECT will let you see the generated SQL SELECT CONCAT (SUBJECT, YEAR) AS subjectyear FROM tablename Second Option using symbol (. WHERE table_schema=DATABASE() AND table_name='foo' AND column_name IN In query, CONCAT keyword used to concatenate two columns. Here is the Dynamic SQL to generate the same query USE mydbĬONCAT('SELECT ',column_name,' AS name FROM ',table_name) For this example, let's suppose the following If the intent is to select some columns and generate a single column list of the distinct values, then Dynamic SQL is needed to produce such a query. This would produce a distinct list of names. Nothing could be more elegant that using UNION SELECT her_name AS name FROM foo In this instance, what makes UNION an absolute must is the merging of three columns into a single column. : Calculating values from three related tables, without using join or union.Others have submitted answers trying aggregation to collect data without using UNION Where one of the dictionary views: SELECT (SELECT := 0) r, INFORMATION_SCHEMA.COLUMNS t Using a constant expression: select 1 line Using an auxiliary table: create table aux(line int) Of course there are different ways to create a table containing the three rows with values 1,2,3: Pivot Query: select elt(aux.line,foo.his_name,foo.her_name,foo.other_name) all_name Insert into foo(his_name,her_name,other_name) values ('four','five','six') Insert into foo(his_name,her_name,other_name) values ('one','two','three') MySQL 5.6 Schema Setup: create table foo ( Instead the ELT function one can use IF or CASE. Each query row can be filled by the appropriate data. Now we have three rows in our query for each row in the base table foo. We want to create 3 rows for each row of the foo table, so we create an auxiliary table containing three rowsĪnd (cross) join it to the foo table. So MySQL does not have such an "elegant" way to unpivot a table.Ī way to do such unpivoting without the use of UNION can be done buy using a join. So if table_references equals foo the query cannot contains more rows than the table foo. Neither the WHERE, GROUP BY, HAVING, LIMIT, SELECT, INTO, FOR UPDATE nor the LOCK IN SHARE MODE clause can increase the number of rows defined by the FROM clause. This is the syntax diagram of the select statement Oracle you can use the following statement to make columns to rows It is unclear to me what is a "more elegant way".
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |