I have a database with many columns and sometimes I need to select quite a few.
Selecting all columns would be too much data. So lets say that:
DESC table_name
gives ordered column names, for example (A,B,C,D,E,F,G,H,I,J....). Is it possible that instead:
SELECT C,D,E,F FROM table_name;
I do something like this:
SELECT [3:6] FROM table_name
I know it makes no difference in this example, but I need to select over 40 columns with long names.
No, you can't SELECT [3:6] FROM table_name What do you think this is, some kind of modern computer language with sequences and ranges as first class data types? :-) :-). This is SQL.
You can, as a commenter pointed out, fetch the names of the columns in the table and then programmatically generate your SQL queries. This is, of course, something a bunch of different data-access-object packages do automatically.
Related
With this query on mysql I can see the delete_priv value
select user,Delete_priv from user ;
I see there is a lot of _priv columns, how to see all priv values?
I have tried
select user,*_priv from user ;
select user,%%priv%% from user;
But is wrong syntax.
No.
In standard SQL, you need to enumerate all names of the columns you want to select. Or, you can use select *, and get all the columns in the table.
Alternatively, you can use dynamic SQL: that is, write a query that searches information_schema.columns and generates the list of columns that matches your filter, concatenate a query string from that, then execute it. But this is really a more complex beast, that is probably overkill for your use case.
I have a table with some denormalized data for a specific purpose (don't ask), so it has several hundred columns. There is a primary key.
This table is updated weekly, but most id:s will have the same data as the week before.
Now, I need to store all record versions in a history table, i.e. if record with id X is added week N, no changes week N+1 but some data changed week N+2 and N+3, then the history table should contain three records: Those from weeks N, N+2 and N+3.
It's technically easy to write the appropriate insert query, but it would involve comparison of each column, so it will be a very long SQL query. I'm sure it would work, but...
Is there any way in MySQL to compare ALL columns without explicitly writing ...or t1.col1 <> t2.col1... for each column? I.e. something like ...t1.allcolumns <> t2.allcolumns..., like comparing the entire row in one go?
I'm pretty sure the answer is no, but... :-)
You can write a program (in your favourite programming language) to build the query. The program would look in the schema for the database, find all the columns of the table, and construct the query from that. I don't think it is possible to do that in plain SQL, but even if possible, plain SQL is probably the wrong tool.
You can use the row-values syntax, but you still have to name all columns:
(t1.col1, t1.col2, ...) <> (t2.col1, t2.col2, ...)
Update 1
Check this out: https://www.techonthenet.com/mysql/intersect.php
Intersect t1 and t2. Result = rows on both tables.
Select all fiends from t1 not in your intersect result.
Sorry for the lack of code, I don't have time to elaborate, but that's the idea.
Is it possible to retrieve the count of the number of columns a query returns? This can be easily done with a bound scripting language such as php, but I'm looking for db only solution.
Example:
CountCols(SELECT 'a','b','c')
=> 3
CountCols(SELECT * FROM information_schema.session_variables)
=> 2
Would this work for you?
select
count(*)
from
`information_schema`.`columns`
where
`table_schema` = 'my_table_schema' and `table_name` = 'my_table_name';
You only need to use table_schema if the table name exists in more than one database.
Based on your response comment, you are looking to count a dynamic number of columns. You may be able to do this with a temporary table, but you cannot access the data of a temporary table without possibly installing a patch.
Of note, there is a similar outstanding SO question asking how to select columns from a temporary table.
Well if you want to know the columns in a table just do:
DESCRIBE `table_name`
Otherwise there is no "real" way to get the number of columns in a select query since other than selecting * you select certain columns --> so you will know how many columns you are selecting.
You'll find your answer here most likely: http://dev.mysql.com/doc/refman/5.0/en/columns-table.html
Write a query off of that that takes a table name param and then query for columns of that table and sum that up.
In an oracle book I read that when when we perform SELECT by joining 2 or more tables, if tablename is used before the column name SELECT works faster.
Eg:
SELECT table1.name, table1.dob.... instead of SELECT name, dob....
Is it the same way in MySQL?
EDIT
I know that this is a good practice when there are identical field names. What i was thinking was about the performance point of view even if there are no identical field names
I dunno about performance, but it is a good practice, especially when joining tables. Joined tables could have for example identical field names, and the query will then fail. You can also use aliases if your table names are too long:
SELECT t1.name, t2.dob FROM table1 t1 JOIN table t2 ON ...
From the efficiency point of view, Oracle and MySQL compile the SQL to an internal representation before executing it, so I don't think there must a significant difference in execution time as they will decide the table from the fields name if they are not specified. The time difference will be at compilation time, where they deduce the tables for each field.
In fact, I personally doubt the fact that Oracle executes faster if the table names are specified!
It's not good practice when the field names are the same, it's good practice all the time. It's not about efficiency, it's about your query not breaking when someone else adds fields to one of the join'd tables with overlapping names, so your stuff works in six months time, not just today.
I want to write a query to select from a table all rows with the word "piggy" in a column called Description.
SELECT * FROM table WHERE ...?
Thank you!
In MySQL, % is a wild-card. You don't use wild-cards with the = operator but with the LIKE operator.
SELECT * FROM table WHERE `Description` LIKE "%piggy%"
http://dev.mysql.com/doc/refman/5.0/en/string-comparison-functions.html
select * from table where description like '%piggy%'
will select and return all rows where the word piggy is part of the value of the column. If you want to count how many rows then:
select count(*) from table where description like '%piggy%'
select * from table where description like '%piggy%'
As mentioned several times you need a LIKE query for this. I would only warn that this is going to be terribly slow in case of a InnoDB table as it doesn't support fulltext scans. If you want better performance with a LIKE, then you should use MyISAM.
Anyway, if you want to implement a search engine, better look for existing API's. I don't know what programming language you're using, but if it was Java, I'd recommend Apache Lucene.