I have to find the column name which has a particular value. For example : I have to find all the column names which has a value 'Availability' stored in that particular column.
To find column name that contain availabvility you could use INFOMATION_SCHEMA.COLUMNS:
SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME LIKE '%Availability%';
or sys.columns
SELECT *
FROM sys.columns
WHERE Name LIKE '%Availability%';
EDIT:
To find specific value in all tables you could use ApexSQL Search Plugin
Text search
Search for text, numbers, dates and unique identifiers in your tables
and views
Image from: http://www.apexsql.com/sql_tools_search.aspx
I don't think so, you have any shortcut to find the value level check. You may need go and check the respective tables and use pattern matching(i.e. like '%availability%') to find the value.
My shortcut would be: You should have general idea about you possible column names which can contain the expected availability value. Then use lad2025 data dictionary queries to find the relevant tables and the column names. Then you can go ahead and run a separate select query on each table/ column.
Related
I want to find all the table names where i have a certain value, like:
SELECT table_name FROM information_schema WHERE column_name = "MyColumnName" AND MyColumnName = "value";
I have problem searching for a value in the column, how should i write the last part..
AND MyColumnName = "value";
..?
You cannot do this in 1 query. Your first query should identify all the tables that you need to query. You then need to query all the tables and you can either do this by hand or write a dynamic sql script to generate the table queries. I'm not going to do this for you since this looks like coursework (it's surprising how often this comes up - must be the time of year so I'm sure you can find further help if you search SO)
Im building a simple search system, I have a simple form and I'm doing a query like this:
Select * from table where column_a like'%term%' or columnn_b like '%term%' or column_c like'%term%';
It is possible to determine which column was that the string %term% match (without using a bunch of if statements)?, actually I'm using CakePHP, but at this point I will not care if I need to build the query manually.
No, the identity of the matched column is lost in the evaluation of your or clauses. You'll need to do some post-processing (i.e., the "bunch of if statements" you were trying to avoid) to identify exactly which column in your result set matched.
I have a MySQL table named data where the name of a column is field_id_# (where # is a number from 1 to 129). I also have another table named fields with columns field_id (with only the # of the corresponding field in table data - That means just the number # not field_id_#) field_name and field_label. Now I would like to run a query like this:
SELECT data.field_id_1 AS fields.field_label1,
data.field_id_2 AS fields.field_label2
[...]
I don't know if this is possible or not and if so, how to do it.
Can someone help me with that?
Thanks for your help.
Please use the meaningful names directly for your column definitions! All your selects and program parts that access the database will be readable. They're surely not readable with the naming convention you describe here.
For an immediate solution you can build the SQL Statement dynamically/programmatically, or you generate views where the columnnames are replaced with the meaningful ones.
How to get a highest value from the second column in a table, without knowing column name? In a single SQL query?
I got a set of tables where the value I'd like to get is always in a second column (that's part of the way tables are always made), but the name of this column is different in each table. So I could use some simple SQL query that can obtain this value. Any ideas? I know it's doable with PHP and additional query to extract the names of columns, but I'm looking for elegant, purely SQL solution :)
It's gonna be a tedious process. But, if it's necessary, you may use ORDINAL_POSITION column in the information_schema.columns table to get the Nth column name.
SELECT column_name
FROM information_schema.columns
WHERE table_name = "your table name"
AND ordinal_position = 1 --replace 1 with your N
Check this.
The obvious solution is to change the data structure. making it relational and normalized.
In a properly designed database such a question like your just simple can't be.
There is always one field which you can query for the highest value. And position in the field list absolutely doesn't matter.
As a side effect of refactoring your database structure, you will have a solution for the hundreds other problems you will have in the future.
I have an automatically generated SQL database.
I don't know the name of the fields, and I don't know the value of the fields; I just know which number of register I need to get and with number of field of that register.
For example, if I need to obtain the fifth field of the second register of the table "Table1" of the database, which SQL query should I do?
Rows in a table in a database are formally unordered, though they are, of course, stored in some order. There's no way in SQL to refer to columns in a table by position; you must know the name of the column.
Since you know the table name, you can interrogate the system catalog to learn the columns in the table, and therefore the second column name in the table (assuming it isn't a single-column table).
However, if you don't know the schema of the tables, you can't do anything meaningful in the way of querying the data. You have to know what the columns mean to know what the query is going to do.
Clearly you can run some query on the table (once you know the column name you're after) and then collect two rows of data; the second row is the one you're after.
...
There's a half-cheat that you can use which will work if your database access language returns you rows with the values for each row in an array - as in Perl with DBI, or PHP, or ...
SELECT * FROM Table1;
This will collect all the data (including column 5, assuming there are that many columns), and your fetch operation may return the values represented by * into an array, and you can then look at the value in the fifth element of the array for the second row to see the data. In many SQL DBMS (I don't know about MySQL specifically), you can even use an obsolescent notation to order by the fifth column:
SELECT * FROM Table1 ORDER BY 5;
The 5 here refers to the fifth column in the result set which, given that this is selecting all columns from a single table, means the fifth column of the table.
However, running blind like that is a ridiculous proposition for the long term. You must understand the schema and its interpretation to be able to use a database sensibly.
You could try:
SELECT *
FROM information_schema.COLUMNS
WHERE information_schema.COLUMNS.TABLE_SCHEMA = '<DATABASENAME>'
AND information_schema.COLUMNS.TABLE_NAME = '<TABLENAME>'
ORDER BY information_schema.COLUMNS.ORDINAL_POSITION ASC
This would give you the table metadata, including column names and types.
can you not do it thru PHP (or your choice):
$i=1;
while($row = mysql_fetch_row($res))
{
if ($i == 2)
{
echo $row[4];
}
$i++;
}
Presumably you have access to the database?
Can't you do:
SHOW CREATE TABLE Table1;
The order of the columns returned should give you the names of the fields which you can then use in a query.