Show table status in a subquery? - mysql

Is this not supposed to work in MySQL?
select * from (show table status like '%fubar%') as t1;
or even
select name, rows from (show table status like '%fubar%') as t1 where rows>0;
This is the error I'm receiving:
ERROR 1064 (42000): 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 'show table status like '%fubar%') as t1' at line 1
Can show table foo like '%something%' or show tables like '%something%' not be used in a subquery in this way? How else could you select from all tables matching a certain pattern?

SELECT table_name as name, table_rows as rows FROM information_schema.tables as t1
WHERE table_rows > 0
Here is an alternate way of retrieving the information you are looking for.

Related

SQL syntax error on sql 5.6.17

IF NOT EXISTS ( SELECT name FROM person)
SELECT id FROM connection
I get:
1064 - 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 'IF NOT EXISTS ( SELECT name FROM person) SELECT id FROM connection' at line 1
What's wrong?
I also tried
IF NOT EXISTS(SELECT name FROM person)
BEGIN
SELECT id FROM connection
END
and get the same error.
That's not a correct way. Rather try like
SELECT id FROM connection
WHERE NOT EXISTS ( SELECT name FROM person)
IF can only be used in stored procedures. To do this in a regular query, do:
SELECT id FROM connection
WHERE NOT EXISTS (SELECT name FROM person)
However, I wonder if this is really what you want. This will return all connection IDs if the person is empty, and return nothing if the person table has any rows. It doesn't relate the rows in the two tables to each other -- you would use a JOIN or correlated subquery for that.

Retrieve certain fields from a table using a column value where table name is unknown

I am trying to get the cost and itemno of a product which I get dynamically from the user. So I don't know under what table the product is present.
SELECT cost,itemno FROM inde.columns WHERE column_name ='previousItems';
Where inde is the schema name and previousitem is the product name which i get dynamically from the user in a jsp.
I get the error message as sql syntax wrong.The following is the error message
javax.servlet.ServletException: com.mysql.jdbc.exceptions.MySQLSyntaxErrorException:
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 ')' at line 1
replave your query by this
SELECT cost,itemno FROM inde WHERE column_name ='previousItems';
this if inde is the name of the table
You can search unknown tables by column name with below query:
SELECT DISTINCT TABLE_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME IN ('COLOM_NAME_TO_SEARCH')
AND TABLE_SCHEMA='YOUR_DATABASE_NAME';

mysql insert - select syntax error

This is my query -
$q = "CREATE TEMPORARY TABLE tmp SELECT * from category c WHERE category_id IN (SELECT category_id FROM category_to_store WHERE store_id = '".(int)$from_store_id."');
ALTER TABLE tmp drop category_id;
INSERT INTO category SELECT 0,tmp.* FROM tmp;
SET #last_id_in_category := LAST_INSERT_ID();
select #last_id_in_category;
DROP TABLE tmp;"
mysql_query($q);
I am getting this error on execution
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 'ALTER TABLE tmp drop category_id; INSERT INTO category SELECT 0'
at line 2 Error No: 1064
But when I run the query in database directly then I am not getting any error.
Please help me !
From the php docu on `mysql_query'
mysql_query() sends a unique query (multiple queries are not supported) to the currently active database ...
So basically split your queries to multiple calls of `mysql_query' each with a single query and you should be fine.

How to check if a column exist inside a table, and preform action based on the result

I use MySQL 5.5.16.
I'm trying to check if a certain column exists in one of my tables. If it is I want to update its type & collation and if it isn't I want to add it to the table.
I've read similar posts Here and Here about IF EXISTS statement. But I'm getting an error with my query:
IF EXISTS (SELECT * FROM information_schema.columns WHERE table_name = 'e4s_account_details' AND column_name = 'accountID') THEN
ALTER TABLE 'e4s_account_details' CHANGE 'accountID' 'accountID' INT(10) NOT NULL AUTO_INCREMENT;
ELSE
ALTER TABLE 'e4s_account_details' ADD COLUMN 'accountID' INT(10) NOT NULL AUTO_INCREMENT;
END IF;
The error I get is:
1064 - 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 'IF EXISTS (SELECT * FROM information_schema.columns WHERE table_name = 'e4s_acco' at line 1
BTW, If I only query this:
(SELECT * FROM information_schema.columns WHERE table_name = 'e4s_account_details' AND column_name = 'accountID');
I get no error and the table+column information is returned.
You can't use this syntax directly with select query instead you can make stored procedure or query with information schema table for doing same.

Modifing select query to delete query

The following select query works fine:
SELECT * FROM JBPM_JOB job WHERE job.ACTION_ IN (SELECT ID_ from JBPM_ACTION WHERE ACTIONEXPRESSION_ LIKE '%#{reminderAction.addAsyncProcessReminder%warning%');
However, when I try to delete the rows retrieved here, it fails
DELETE FROM JBPM_JOB job WHERE job.ACTION_ IN (SELECT ID_ from JBPM_ACTION WHERE ACTIONEXPRESSION_ LIKE '%#{reminderAction.addAsyncProcessReminder%warning%');
What is wrong here?
The error message is:
ERROR 1064 (42000): 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 'job WHERE job.ACTION_ IN (SELECT ID_ from JBPM_ACTION WHERE ACTIONEXPRESSION_ LI' at line 1
You need to specify you are deleting from the alias table, so use:
DELETE job FROM JBPM_JOB job WHERE job.ACTION_ IN (SELECT ID_ from JBPM_ACTION WHERE ACTIONEXPRESSION_ LIKE '%#{reminderAction.addAsyncProcessReminder%warning%');
i have tested the query in sql server. works fine but there is possible that the values you are deleting have some relationship with other table like PK and FK.
if they have then you have to delete the records from those tables too..........