Ask me a question about a database statement
SELECT `id` , `time` , `title`FROM `think_infor`WHERE `type` LIKE '$k-%'UNION ALLSELECT `id` , `time` , `title`FROM `think_infor2`WHERE `type` LIKE '$k-%'ORDER BY `time` DESCLIMIT 0,6
You can use this statement to query the latest six pieces of information in Tables 1 and 2. But how can I determine which table the record returns?
Or is there any other method that can be implemented and you do not want to query it multiple times?
Reply to discussion (solution)
You just need to add the table name?
However, can you get the latest 6 pieces of information for tables 1 and 2?
Select a. id AS aid, A. time AS atime, A. title AS atitle, B. id AS bid, B. time AS btime, B. title AS btitle
FROM think_infor as a, think_infor2 AS B
Where a. type LIKE '$ k-%' and B. type = A. type
Order by a. time desc limit 0, 6
Select a. id AS aid, A. time AS atime, A. title AS atitle, B. id AS bid, B. time AS btime, B. title AS btitle
FROM think_infor as a, think_infor2 AS B
Where a. type LIKE '$ k-%' and B. type = A. type
ORDER ......
This is similar, but there are repeated records in Table.
SELECT `id` , `time` , `title`, 1 as cpfromFROM `think_infor`WHERE `type` LIKE '$k-%'UNION ALLSELECT `id` , `time` , `title` , 2 as cpfromFROM `think_infor2`WHERE `type` LIKE '$k-%'ORDER BY `time` DESCLIMIT 0,6
This statement is not clearly written.
Reference the reply from yiwusuo on the second floor: select. id AS aid,. time AS atime,. title AS atitle, B. id AS bid, B. time AS btime, B. title AS btitle
FROM think_infor as a, think_infor2 AS B
Where a. type LIKE ......
Order by a. time DESC followed BY having count (A. id) = 1
Isn't it repeated?
I am confused. let me talk about what I want.
Table A and Table B have the same structure and different data. I want to fuzzy query A field 'type' at the same time ', select the first 6 records that meet the conditions in the two tables (sorted by 'time)
Can't you write it yourself?
SELECT `id` , `time` , `title`, 'think_infor' as tbl_name FROM `think_infor` WHERE `type` LIKE '$k-%'UNIONSELECT `id` , `time` , `title`, 'think_infor2' FROM `think_infor2` WHERE `type` LIKE '$k-%'ORDER BY `time` DESCLIMIT 0,6
Added an output field tbl_name to differentiate data sources.
Change union all to union to avoid repeated records. If you do not add more sources, repeat them.
Can't you write it yourself?
SQL code
?
123456789
SELECT 'id', 'time', 'title ', 'think _ in' as tbl_name FROM 'Think _ in' WHERE 'type' LIKE '$ k-%' UNIONSELECT 'id', 'time', 'title ', 'think _ in ......
SELECT fields that do not exist.
Do you mean 'think _ infor 'as tbl_name?
So do you think that SELECT 'id' as iidd FROM 'Think _ infor 'can be like this?
I'm sure I can. isn't it just a name?
I won't talk about it anymore.