I am trying to select all rows from all tables except for 2 tables in SQL for use in PHP calculations. I am trying run the follow query:
SELECT MovieID, Rating FROM information_schema.tables WHERE table_schema = 'moviedb' AND TABLE_NAME <> ('movies', 'users');
This is returning "ERROR 1054 (42S22): Unknown column 'MovieID' in 'field list'" in SQL.
All tables except 'movies' and 'users' have identical columns but varying data and amounts of data (some are all null).
I have tried using EXCEPT and it doesn't seem to work on the version of SQL we are running. It's for a school project on a school server so I have to use the version available.
Related
How does one display the temporary column name (which is not present in the table) ONLY, when using the case statement?
For example:
select my_column_name,
case
when X <=10 then 'A'
when X >=11 AND X <=19 then 'B'
when X >=20 then 'C'
else 'Some error occured'
END AS my_column_name
from table_name
now if I execute this MySql workbench gives this error:
Error Code: 1054. Unknown column 'my_column_name' in 'field list'
this will work if I give column names which are in the table or just give the * but it does not work if I just want the temporary name/alias (I'm not sure what this column is called)
I am working on a mysql procedure that I can run on all of my client environments however I am running into one issue. Some of my clients have a particular column on the database and others do not.
I am trying to use update ignore but I still get an error if the column doesnt exist.
I know I can query information schema for a count where that column exists, but I'm wondering if there is a more simplistic way to achieve this.
update ignore table1 set columnA = null;
ERROR 1054 (42S22): Unknown column 'columnA' in 'field list'
You have two options:
First solution: Check first if the column exists.
SELECT t.table_name, (c.column_name IS NULL) AS columnA_present
FROM INFORMATION_SCHEMA.TABLES AS t
LEFT OUTER JOIN INFORMATION_SCHEMA.COLUMNS AS c
ON t.table_name = c.table_name and c.column_name = 'columnA'
WHERE t.table_name = 'table1';
Second solution: Run the UPDATE and catch the error. Check if the error is 1054. If so, then skip it. If not, then it's some other error so report it.
Below is the query I used to select multiple columns from one of my tables.
I get the column names from user to select from the table.
If the user gives a wrong column name it shows Unknown column error. How can I check if that column exists in the table before going to select?
SELECT `address_id`,`address_firstname`,` afserfw`
FROM `patsm_addresstable`
WHERE `address_id`='28'
LIMIT 0, 25
This would give the following error:
#1054 - Unknown column ' afserfw' in 'field list'
This query will give you a list of columns in a particular table with their datatypes.
SELECT COLUMN_NAME, DATA_TYPE
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = DATABASE()
AND TABLE_NAME = 'tablename'
You can, when constructing a query against any table, use this resultset to determine whether a given column exists.
But be careful: a web application allowing end-users to give you column names is hard to make secure against intrusion by cybercrooks.
I'm trying to craft a straight SQL (not stored procedure) query that finds all databases in a MySQL database server with a table whose name matches a pattern and has a certain collation, and then use the list of those database names and table names to get a specific value out of the tables that match the pattern.
For this query:
I do not know the names of the databases on the server (they must be queried)
I do not know the names of the tables in the database that could match (they must be compared against a pattern of %options)
For example, if I have 3 databases:
DB1 with tables:
something
f_options (containing a row where the value for a column called option_name is 'test' and the value for the option_value column is 'y')
DB2 with tables:
something
DB3 with tables:
something_else
zeoptions (containing a row where the value for a column called option_name is 'test' and the value for the option_value column is 'z')
Then I want a query that:
Finds databases with tables who match the collation and naming scheme
Select the values of the rows where the column called option_name has a row whose value is "test" in the column
For example, in the above 3 database tables, it would see Database 1 and 3 have a matching table, and it would return the values 'y' and 'z'
I'm trying to do this without using stored procedures or views (just a simple SQL statement).
So far I've gotten a working system that does 1. By querying the MySQL information_schema table, I can get the names of the databases + matching tables using this:
SELECT s.dbname, s.tablename FROM ( SELECT
`information_schema`.`TABLES`.`TABLE_SCHEMA` dbname ,
`information_schema`.`TABLES`.`TABLE_NAME` tablename FROM
`information_schema`.`TABLES` WHERE `information_schema`.`TABLES`.`TABLE_NAME` LIKE
'%options' AND `information_schema`.`TABLES`.`TABLE_COLLATION` LIKE
'utf8mb4_unicode_ci' ) as s;
Now the problem is figuring out how to write a SELECT statement who uses the above SQL statement as a subquery to populate the FROM
Right now this is what I have (though there's an obvious SQL error in how the FROM statement currently works). If I can solve the FROM statement issue I can finish the rest
SELECT `s.dbname`.`s.tablename`.`option_value` FROM `s.dbname`.`s.tablename`
( SELECT `information_schema`.`TABLES`.`TABLE_SCHEMA` dbname ,
`information_schema`.`TABLES`.`TABLE_NAME` tablename FROM
`information_schema`.`TABLES` WHERE
`information_schema`.`TABLES`.`TABLE_NAME` LIKE '%options' AND
`information_schema`.`TABLES`.`TABLE_COLLATION` LIKE 'utf8mb4_unicode_ci' ) as s
WHERE `s.dbname`.`s.tablename`.`option_name` LIKE 'test';
Which can be simplified to
SELECT `s.dbname`.`s.tablename`.`option_value` FROM `s.dbname`.`s.tablename`
( the working query above ) as s
WHERE `s.dbname`.`s.tablename`.`option_name` LIKE 'test';
Does anyone know how I can fix the
FROM `s.dbname`.`s.tablename` ( SELECT ... ) as s
part?
I am working on a WPF application which installs if MySQL is installed,
so before installation I want to check whether mysql.proc table exists or not.
I googled about it and ended up with a query
select * from information_schema.Tables
where Table_schema = Schema() and Table_Name = 'mysql.proc'
This query returns an empty row.
I also tried simple select statement
select * from mysql.proc,
and this returned a table with the names of all the stored procedures, but if this table didn't exists then it throws an exception in the c# code.
So is there any way that I can fire a query from c# and get a boolean value depending on whether mysql.proc table exists or not?
Try SHOW TABLES FROM mysql LIKE 'proc'. If there are no result rows, the table doesn't exist. If there is one row, the table exists. Note that this approach isn't portable across RDBMSs, though that doesn't seem to be a concern of yours.
As for your first query, SCHEMA() returns the default database, so if it's not "mysql", the query will fail. Likewise, data in the Table_Name column doesn't include the database name, so comparing to 'mysql.proc' will always fail.