Query two tables with different number of fields, different names of SQL statements

Source: Internet
Author: User
This SQL statement can query for two different structures of different tables and can tell which records belong to which table
has two tables
The Table1 structure is as follows
ID Title Content

The table2 structure is as follows
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 DESC '
This statement allows you to implement the query.
For example, in ASP, the
Set Rs=conn.execute (SQL)
While not rs.eof
......
......
Response.Write rs ("tbl") ' This value is to determine which table the data is
......
......
Rs.movenextwend
Analysis of the above SQL statements, in fact, through as the two tables in the field name uniform, the number of fields is not enough in the (') brackets, that is, empty strings replaced, of course, if you are willing to also use a non-empty string instead (' This is bigclass belong to Table2 '), This also makes it easy to distinguish between the data in which table. This SQL statement cleverly uses the ' tbl1 ' as TBL to determine which table the statement belongs to, in order to process the data, especially for site search, search for different tables in a page display results, according to different tables to different ID values to different tables corresponding to the display page, Some want to use readnews.asp some to use other.asp this kind of page, use up very convenient.
Welcome everybody to discuss together!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.