Query the number of fields in the two tables, different SQL statements

xiaoxiao2021-03-06  40

Source: Unknown Author: Unknown Recommended rating: Visits: 11

This SQL statement can be queried by different fields of different structs in different tables, and which record is to which the table belongs to the two table Table1 structures The following ID Title Content Table2 Structure The following ID Content Bigclass SmallClass SQL = "SELECT ID , Title, Content, '' AS Bigclass, '' AS SMALLCLASS, 'TBL1' AS TBL from Table1 Where Title Like '% "& Keyword &"%' or Content Like '% "& Keyword &"%' Union Select ID, 'AS Title , Content, Bigclass, SmallClass, 'TBL2' AS TBL from Table2 WHERE Content Like '% "& Keyword &"%' Order By ID "This statement can realize the query. Take ASP as an example, then through SET RS = conn.execute (SQL) While Not Rs.eof ...... response.write RS ("TBL") 'This value is which table is determined which table is ...... RS. MoveNextWend analyzes the above SQL statement, in fact, through the AS to unify the field names in the two tables, the number of fields is not enough, that is, the blank string instead, of course, if you are willing to use non-empty string instead Such as ('this is bigclass belong to Table2'), which is also easy to distinguish between the data in which table is. This SQL statement is cleverly uses the 'TBL1' AS TBL this to determine which table belongs to, in order to process the data, especially for the site search, search the results in one page when searching the different tables, according to different tables Passing different ID values ​​into different tables corresponding to the display page, some to use readnews.asp to use Other.asp this page, which is very convenient. Welcome everyone to discuss!

转载请注明原文地址:https://www.9cbs.com/read-70404.html

New Post(0)