number of rows in a stored procedure - mysql

is it possible to do conditional select based on a number of rows in a stored procedure?
E.g. if select * from table1 has no records, then do select * from table2?

Try this:
SELECT col1, col2, ..., coln FROM table1
UNION ALL
SELECT col1, col2, ..., coln FROM table2
WHERE NOT EXISTS (SELECT * FROM table1)
This of course assumes that both tables have exactly the same structure.

Related

MySQL Conditional insert to one table if a certain row does not exist on another table

Is it possible to do an insert query like this?
INSERT INTO `table_1`
VALUES ('val1','val2','val3')
WHERE (
SELECT COUNT(*)
FROM `table_2`
WHERE col1='somevalue'
)=0;
You can do this using insert . . . select:
INSERT INTO `table_1`(col1, col2, col3)
SELECT col1, col2, col3
FROM (SELECT 'val1' as col1,'val2' as col2, 'val3' as col3) t
WHERE NOT EXISTS (SELECT 1
FROM table_2 t2
WHERE t2.col1 = 'somevalue'
);
Notes:
This seems really strange. Limiting inserts into table_1 when no row exists in table_2 with other values.
NOT EXISTS is more efficient than using COUNT(*).
You should always list the columns when using INSERT.

mysql storing subquery in column and performing where condition

i am performing a sub query in mysql which is like
select col1, col2 , (select col3 from table2) as 'data'
from table1
where not data is null
how should i get data in where clause. IS it POSSIBLE
One way to do this is :
SELECT *
FROM (
select col1, col2 , (select col3 from table2) as 'data'
from table1
)t
WHERE data IS NOT NULL
As you see there I have created on derived table t for your query, now result of your query is treated as Table(temp table) and having columns as col1,col2 and col3, Using this result set we can able to access col3 in where clause .
Note - assuming that select col3 from table2 returns single value as per OP's comments
Use cross join:
select t1.col1, t1.col2, t2.col3 as data
from table1 t1 cross join
(select col3 from table2) t2
where t2.col3 is not null;

MYSQL: Merge two tables into one, with union

I have to make a table out of two other tables (and use union). The query that works is:
SELECT * FROM tabel1
UNION
SELECT * FROM tabel2
Now what i have to do is put this result(data) into table3 (a table i already have, with the same columns as in table1 and table2).
Who can help me?
INSERT INTO table3
SELECT * FROM tabel1
UNION
SELECT * FROM tabel2
since you have the same columns in all three of them ...
In a general case you should work with column lists like
INSERT INTO table3 (col1, col2, col3)
SELECT col1, col2, col3 FROM tabel1
UNION
SELECT col1, col2, col3 FROM tabel2
This way you avoid trouble with auto_increment id-columns. Also You should consider using UNION ALL since UNION filters out duplicate lines and therefore will take longer on large tables.

Multiple select into?

In MySQL, you can write something like
INSERT INTO t1 (col1) SELECT col1 FROM t2
To copy some data over. What if I want to copy some data over from multiple tables? Can I write something like
INSERT INTO t1 (col1) SELECT col1 FROM t2, SELECT col1 FROM t3
?
i think it should be
INSERT INTO t1 (col1) SELECT col1 FROM t2 UNION SELECT col1 FROM t3
EDIT: Now before you go copying data, you may want to verify using
UNION vs UNION ALL
UNION will remove duplicates in the data.
UNION ALL will produce a simple concatenation of the two result sets.

MySQL: How to search multiple tables for a string existing in any column

How can I search for in table_a table_b table_c, which have a random number of columns for a string?
I know this is not proper sql but it would be something like:
SELECT * FROM users, accounts, something_else WHERE ->ANY COLUMN CONTAINS 'this_string'<-
Ty in advance for SO community
Add fulltext indexes to all of the string columns in all of those tables, then union the results
select * from table1 where match(col1, col2, col3) against ('some string')
union all
select * from table2 where match(col1, col2) against ('some string')
union all
select * from table3 where match(col1, col2, col3, col4) against ('some string')
...