I know of the following two ways to show column names of a table:
SHOW COLUMNS FROM tablename
and
DESCRIBE tablename
How can I do a SELECT FROM tablename and also return the columns (If possible)?
The reason I want to do this is because in Nim, the MySQL module doesn't provide any proc for returning an associative array of results that you can reference by column name. You have to get results like x[0]. I'd like to create my own module for this and I don't want to execute two different queries for it.
Related
Let's say I have a table like this
this table is the result of a query from another larger table stored in my database
All I want is to create a table like this one above and specify for each column a custom format and store it into my database
I know that I could do create table mytab as select ... etc
however i don't know how to specify the column formats that I want in mysql
could you please help ?
If you have the query sql, you should be able to do a select into to store the results in a table. Add a LIMIT clause to just store one row. You could then do SHOW CREATE TABLE tablename (from this SO answer) to get the SQL for creating the table. It would be up to you to figure out what your primary key should be.
Assuming with column formats you mean data types: Use CAST to cast to the desired data type.
create new_table as
select
cast( a.metrique as varchar(100) ) as metrique,
cast( b.nombre_de_lignes as int ) as cote_de_lignes, ...
from ...
You may specify columns properties completely or partially. Like there is no SELECT part, and you simply create empty table.
I.e. like
CREATE TABLE table_name ({any definitions allowed in table creation query:
columns specifications, indices, constraints, FKs, etc.})
SELECT ...
In this form each output column in SELECT must have alias which matches according column name defined in CREATE TABLE part. If alias is absent in the structure then a column with the name==alias will be added to the table definition with dynamically formed properties.
I have a complex query which generates a simple single column id list, which I use to count the number of rows. Thereafter I need the IDs generated by the first query to combine with the result set of another ( second ) query. How can I preserve the result set of the first query to achieve the binding with the second query ?
Thank you.
With a temporary table:
CREATE TEMPORARY TABLE tmp_result AS [first long complex query here];
(You may wish to add an index here, depending on the nature of the rest of the work.)
//CREATE INDEX tmp_result_x ON tmp_result(some_col_name);
I'm working on SQL Server to get the datas from one database and compare with another database. The database we have, has around 400's of table. I have to write a query to get all the table names based on the DB name, and having one column name, I have to get the datas from all the tables.
Still now, I have written a query to get the primary key value and to get the table names having that primary key value. My plan is to call the primary key details in first cursor, and within that create another cursor and fetch the details of the table name and column name.
Based on the values retrieved, I have to write a query to fetch the datas like "select * from #cursor_variable_tablename where primarykeyval = #cursor_variable_primarykeyval".
Is it possible to work like this by calling a variable instead of giving the table name.?
Please help me with this. Thanks In Advance.
Not sure what you are trying to achieve here but you can use dynamic sql to execute the query having table name as a variable.
Suppose I have a select query like :
SELECT * FROM tablename
and in the table are columns : field1, field2 and field3
Does anyone know if it's possible to get a resultset with only 1 row with 1 field, with comma separated values of the columns, like this :
"fieldvalue1, fieldvalue2, fieldvalue3"
Problem is that I don't know the column names of the table in advance...
Another problem is that a prepared statement is not the way to go, since all this should be done from within a trigger, and MySQL doesn't allow dynamic cursors/selects inside a trigger.
I have done some research and only came as far as GROUP_CONCATenating the column names correctly. But the problem is, that
SELECT (SELECT GROUP_CONCAT( cols.column_name) FROM (SELECT column_name FROM information_schema.columns WHERE table_name='test_table') as cols) FROM test_table
will return one and the same concatenated string containing the column names, once for each table row, instead of evaluating it as the column names for the outer select statement and returning the actual values.
From what I have read in all of the forums discussing this kind of question (and there were many), there is really no way of getting this to work without prepared statements.
I can only think of one other way to do this, and that would be having a dedicated column on each table, where you concatenate the individual column values on INSERT or UPDATE, so you can simply select this one field instead of the full set of fields.
Seems like you have 3 questions here:
Getting a resultset with 1 row, 1 field: MYSQL has a CONCAT_WS function that works like this:
SELECT CONCAT_WS(',',Field1,Field2,Field3)
That will return "Field1Value, Field2Value, Field3Value"
I'm not sure how you are going to get these column names. Do you need to get them from a sql statement, a string, etc. ? You can get the table names `SHOW COLUMNS FROM tablename'. The Field column will have the column names.
Triggers are available in mysql (added in 5.0.2 I think): http://dev.mysql.com/doc/refman/5.0/en/triggers.html
First, to find out the columns' names in advance, assuming that you have the table's name, you can get them as any other query:
SHOW COLUMNS FROM your_table
Once you have the names you can do:
SELECT CONCAT(field1,',',field2,',',field3) AS newField FROM your_table
I have a table that is indexed on the first 2 columns. Column A is called "directory" and Column B is called "name".
Is there short syntax for my select statement that I can use to return the proper row?
Example:
Can I
SELECT * FROM table WHERE indexname = '/dir/sub-dir/page.html'
or do I have to
SELECT * FROM table WHERE directory = '/dir/sub-dir/' AND name = 'page.html'
If I can use the first example, what does the WHERE clause look like?
Thanks.
If you have an index defined against both columns, the second usage is the proper one. In SQL, you can only define predicates against columns in tables, views, or table valued user-defined functions (which I don't think MySQL has yet). You can't select off an index, but the optimizer will know to use the appropriate index when you include both columns.