Tell me please. It is necessary to make a sample with a database with this name of the table from which the sample is taken, is formed based on the prefix of the table and the data that are extracted from the table.

Tables: Sites, Chats, and Chat_ID (correspondence tables).

I need to get the site id and the number of unread from this site. I do this:

SELECT s.id AS id, (SELECT COUNT(*) FROM КАК ПОЛУЧИТЬ ИМЯ ТАБЛИЦЫ? AS ch WHERE ch.chat_id = c.id AND status = 0 AND type = 'CLIENT' ) AS unread FROM site AS s LEFT JOIN chat AS c ON c.site_id = s.id AND c.operator_id = s.user_id WHERE s.user_id = 1; 

The name of the table consists of the prefix and the site id is chat_1.

Maybe this is not correct - do not throw tapkapi)

    1 answer 1

    It is not right. Instead of a set of Чат_ID tables Чат_ID you need one Переписки table. And in this table you add the field Чат_ID

    • I can have a lot of sites and still write chats of all sites in one table? - Tsyklop
    • @Tsyklop Exactly. Let at least a thousand sites be. - Anton Shchyrov
    • Well, I wrote the request in one line, and you somehow formatted it in a normal form. How or what? - Tsyklop