I am trying to use :
SHOW COLUMNS FROM #__tablename WHERE Field.columnname = 'any value' ;
If I use it without where condition then it retrieves all column names but I want to fetch specified column name.
Please suggest.
You can use information_schema.columns table instead of SHOW COLUMNS statement. The information_schema.columns table is the same as SHOW COLUMNS, but you can work with result-set as with ordinary table. For example -
SELECT * FROM information_schema.columns
WHERE table_schema = 'table name' AND table_name = 'table name';
Specify columns you need and WHERE filter.
You Can use information_schema.columns , and add a filter to fetch column you want, I think it works
SELECT * FROM information_schema.columns
WHERE table_schema = 'table name'
AND table_name = 'table name'
AND column_name = 'Column name'
Related
How do I get the column name of two tables in a single query ?
SELECT column_name
FROM INFORMATION_SCHEMA.COLUMNS
where table_name = 'table_name';
This works for single table. But if I try
SELECT column_name
FROM INFORMATION_SCHEMA.COLUMNS
where table_name = 'table1'
AND
SELECT column_name
FROM INFORMATION_SCHEMA.COLUMNS
where table_name = 'table2';
This throws error.
Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT column_name FROM INFORMATION_SCHEMA.COLUMNS where table_name = 'table2' ' at line 5
Error Code: ER_PARSE_ERROR
Different queries will be put together using UNION ALL (or UNION only if it's necessary to exclude identic values which appear in both queries):
SELECT column_name
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_name = 'table1'
UNION ALL -- here use "UNION ALL" instead of "AND"
SELECT column_name
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_name = 'table2';
Since you want to get data from the same table, you don't need two queries at all.
Just use an IN clause...
SELECT column_name
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_name IN ('table1','table2');
...or use OR:
SELECT column_name
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_name = 'table1'
OR table_name = 'table2';
if you want to select 2 columns from different tables, you need to add some relation between them.
On the basis of that relationship, you can fetch data from two different tables.
Please check the link: https://www.w3schools.com/mysql/mysql_join.asp
Hope it will solve your problem.
Hi everyone I have multiples tables which I want to get tables with specific column name which I am able with the following code:
SELECT COLUMN_NAME AS 'ColumnName'
,TABLE_NAME AS 'TableName'
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'DATABASENAME'
AND COLUMN_NAME LIKE '%columnName_I_Need%' // Example not actual search
ORDER BY TableName
,ColumnName;
Now I want to get the all data from the resulted tables.
For example, get all columns and their data in resulted tables.
This is an example but is not working :
SELECT * WHERE columnName_I_Need = 1
Is this possible with MySQL?
MySQL version: 5.5.5-10.3.23 MariaDB
This is quite complicated. If you used only dynamic SQL, you would need a looping mechanism. My recommendation is to generate the SQL and then run it manually:
SELECT GROUP_CONCAT('SELECT * FROM ', TABLE_NAME,
' WHERE ', COLUMN_NAME, ' = 1'
SEPARATOR ';
'
)
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'DATABASENAME' AND
COLUMN_NAME LIKE '%columnName_I_Need%';
Then copy the statement and run it manually.
The 'FROM' is missing in your query. Also when you specify a column name it should be written inside single quotes.
SELECT * FROM your_table_name WHERE columnName_you_Need = 'colum_id'
This is a simple and basic query to access the data from the table.
SELECT * FROM tablename
WHERE can include a condition here.
* is used to select each and every column.
One can use the column name to access a particular column.
I am using the following SQL command to get all column names from my table in MySQL:
SELECT column_name FROM information_schema.columns where table_name = 'сведения о фильме' and table_schema = 'videokassety2';
It give me all column names in the table 'сведения о фильме', however it sorts the results and gives me the following:
ID Компании
Год выпуска
Название фильма
Номер фильма
Основные исполнители
Характер фильма
However, I do not need sorting, I need the order in which they appear in the table itself, like the following:
enter image description here
How can I get a list of column_nmaes without any sorting?
Thank you
The column ordinal_position has the original column ordering information:
SELECT column_name
FROM information_schema.columns
WHERE table_name = 'сведения о фильме' AND table_schema = 'videokassety2'
ORDER BY ordinal_position;
I need to update a value in all tables where column name and the column value equals to given data
SELECT * FROM(
SELECT table_schema, table_name AS table_name_str
FROM information_schema.columns
WHERE column_name = 'customerID' AND table_schema='testdb'
) AS tb1
WHERE table_name_str.customerID=5
When I try to run this it gives the following error message:
[Err] 1054 - Unknown column 'table_name_str.customerID' in 'where
clause'
SELECT * FROM(
SELECT table_schema, table_name AS table_name_str ,customerID
FROM information_schema.columns
WHERE column_name = 'customerID' AND table_schema='testdb'
) AS tb1
WHERE tb1.customerID=5
Try above code.
You have to specify that column in inner query then only you can put where condition on it in outer query.
Hope this helps.
This can be done in a two step process:
1. Create the UPDATE query string:
SELECT concat('UPDATE ', table_schema,'.', table_name,' SET customerID=<new_value> WHERE customerID=5;' as qry_str
FROM(
SELECT table_schema, table_name
FROM
information_schema.columns
WHERE
column_name = 'customerID'
AND table_schema='testdb'
) AS tb1
2. Run the UPDATE statements:
UPDATE testdb.table1 SET customerID=6 where customerID=5;
UPDATE testdb.table5 SET customerID=6 where customerID=5;
...
...
and so on.
Let me know if this works.
suppose i have three table with names : broker,man and coach.
i want to know that for example 'name' field
is in which table.
how can realize it?
Strange question indeed.
select column_name,table_name
from information_schema.columns
where table_schema = 'your_db' and table_name in ('broker','man','coach') and column_name = 'name'