Dynamic table name in where clause using a field from select

alexander.sivak

I need to insert identifiers (numbers) into a temporary table which satisfy several conditions. I use insert into select structure. One of the conditions is the next. There are tables received_posts_1(id,post_id), received_posts_2(id,post_id)... Each selected identifier is part of a table name with received posts. I need to add an and part into the where clause of the next form.

and not exists(select 1 from CURRENT_RECEIVED_POSTS_TABLE where id = device_id and post_id = post_id_)

The insertion is in while cycle. The stop condition is a needed count of identifiers to be inserted.

Gordon Linoff

Having multiple tables with the same structure is generally a sign of a poor database design. In general, it is much better to have a single table, with columns that distinguish what you are trying to do.

One approach is to create such a table using a view:

create view received_posts
    select 1 as which, r.* from received_posts_1 r union all
    select 2, r.* from received_posts_2 r union all
    . . .;

You can then use this table in your query.

A more efficient method is to repeat the exists, with the right conditions:

not exists(select 1
           from received_posts_1
           where id = device_id and post_id = 1) and
not exists(select 1
           from received_posts_2
           where id = device_id and post_id = 2) and
. . .

As mentioned in the comments, you can use dynamic SQL if you know which post table you need for a particular invocation of the query.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Select column using different WHERE clause from a different table

Dynamic column name in WHERE clause using MyBatis

Select from JSONB field with WHERE clause

Using the same table on SELECT and WHERE clause

SELECT from table with Varying IN list in WHERE clause

Loop with dynamic table name and where clause stored procedure

Select from table where clause from array from another table

Dynamic SQL with table name from a field

WHERE clause from another table using NOT IN

mySQL SELECT values from a TABLE where the TABLE name is based on a field value

WHERE clause with the same field name

Dynamic select query with where clause

SQL Select * FROM table where date is dynamic

Using select in where clause

Sql select from a table but use another table value as where clause

Select from mysql table WHERE field in '$array'?

SELECT query from field having multiple value with WHERE_IN clause

Using a subquery in the where clause to select 2nd highest date from a table

Mysqli Select query when where clause is using items from another table

Select from table with dynamic name Or Multiple names

Use a string (multivalued field from table) as where clause in SQL query

select * from table where name like %O%

Select * from table_name where distinct

Left Join Where Clause (name matching ID from another table)

Using a subquery in a dynamic where clause

where clause based on a select statement from another table

Combining two SELECT queries from same table but different WHERE clause

How to select from a table with additional where clause on a single column

How to select rows from table that matches where clause on two columns?