12. Database Query Language 1

xiaoxiao2021-03-06  35

The original plan will introduce the ASP built-in ActiveX component from this article, but considering that we will come into contact with a large number of database queries in the future learning, the author temporarily decides to spend one to two paragraphs Everyone briefly introduces the basic knowledge of some database query languages, which is actually a knowledge that I must master the ASP. Whether it is possible to flexibly use a database query language, it will be directly related to the execution efficiency of the ASP program, so you must pay attention to it. I believe many friends have heard of SQL this name. If you are a computer expert, SQL's big name must be like a thunder. So what is SQL exactly? The word SQL is actually an abbreviation for the "Structure Query Language" structural query language, is a tool for organizing, managing, and retrieving data stored in a computer database; it is a specific type of database-relational database . Computer programs that control this database are the DBMS-database management system that we often say. Such as: SQL Server, Oracle, Sybase, DB2, etc. When the user wants to retrieve data in the database, the request is issued via the SQL language, then DBMS processes the SQL request and retrieves the required data, and finally returns it to the user, this process is called a database query, this is also It is the origin of the name of the database query language. SQL is not a complete computer language like C, Cobol, and Fortran languages. SQL does not have an IF statement for condition testing, nor does it use the GOTO statement for program branches and cyclic statements for or do. Specifically, SQL is a database sub-language, and the SQL statement can be embedded in another language, thereby having data inventory. SQL is also non-stringent structural language, its syntax is closer to the English statement, so it is easy to understand, most SQL statements are interested in it, read as a natural language. SQL is also an interactive query language that allows users to directly query storage data, using this interaction characteristic, users can answer fairly complex issues in a very short time, and the same problem If the programmer will write the corresponding report programs may be It takes a few weeks or even longer. In most ASP applications, we will come into contact with the database, and we used the standard syntax used to perform database operations when writing ASP applications, so the importance of SQL syntax is self-evident. Below, we will start from the most common SQL statement SELECT, and learn SQL step by step. The query is the core of the SQL language, and the SELECT statement used to express the SQL query is the strongest SQL statement, which retrieves data from the database and supplies the query results to the user. In this article we will establish a simple database called Tianjiao, which stores a sales record table called Sales, as shown below:

Name Gender Salary Sales Target Sale Area Subject Male 250080009000 Shanghai Thunder Men 2000800010000 Sichuan Snow Children 250050006000 Guangzhou Gu Yi 260090009800 Dalian Dalian Dalian 200040004000 Tianjin 天 男 40002000020000

In this table, there are six columns: name, gender, salary, sales target, sales, region, first we use the SELECT statement to list name, sales goals and sales: SELECT name, sales target, sales from Sales results are as follows:

Name sales target sales book students 80009000 Wu Champions 100009999 Thunder 800010000 Xueer 50006000 Gu Yixia 90009800 Android 40004000 天 2000020000

Then we list all the names, sales objectives and sales: SELECT name, sales target, sales from sales where gender = "Male" results: name sales target sales books 80009000 Wu Champions 10000999 Thunder 800010000 Gu Yi 90009800 天 2000020000

Next, we have a relatively complex query, list all the sales targets, sales targets and sales of sales greater than the sales target, and sort by sales target. SELECT Name, Sales Target, Sales Form Sales Where Sales> Sales Targets and Gender = "Men" ORDER BY Sales Target Results As follows:

Name sales target sales books 80009000 Thunder 800010000 Gu Ye 90009800 天 2000020000

12

Everyone can see that for simple queries, SQL SELECT statements and English syntax are very similar, let's analyze the complete format of the SELECT statement, which includes six clauses, where SELECT and FROM clauses are necessary, other clauses can be The function of each clause is as follows:

1. SELECT clause lists all data items that require SELECT statements. It placed at the beginning of the SELECT statement, specifying the data item to be retrieved. These data items typically use the selection table, ie, a set of selection options. According to the order from left to right, a column generated by each selection item, a selection may be the following items:

(1), the column name: Identify the "Column in the table in the FROM clause. If the column name is used as the selection, SQL removes the value of the column directly from each row in the database table, and then places it in the corresponding line of the query result.

(2), constant: Specifies that this value is placed in each row of query results.

(3), SQL expression: Description Must be calculated according to the specified value in the query result.

2, the FROM clause lists the tables containing the data you want to query, which consists of a set of comma-separated table names by keyword from. Each indicates that it represents a table that includes the query to retrieve data. These tables are the table sources of this SQL statement because the query results are from them.

3, the WHERE clause tells SQL only to query the data in some rows, these rows search criteria descriptions.

4, the Group By clause specifies the summary query, ie not generating a query result for each row, but a similar row is packet, and a summary result is generated for each group.

5, the Having clause tells SQL only generates the result of some groups obtained by Group By, as the WHERE clause, the group you need is also specified in a search condition.

6. The ORDER BY clause sorted the query results in one or more columns. If this clause is omitted, the query result will be disorderly.

The following will provide a simple but practical ASP program to use the SQL statement query for your reference.

In order to make everyone more clearly understand the application of SQL syntax in the ASP, we first write all the cores of the query into query2Table, then call the SUB using the ASP server-side inclusion function. Please scrapped the following statement to the Notepad, saved as subdbtable.inc file and placed in a virtual directory asptest:

<%

SUB Query2Table (InputQuery)

SET ConnTemp = Server.createObject ("AdoDb.Connection")

CONNTEMP.OPEN "DSN = student; uid = student; pwd = aspmagic"

Set Rstemp = ConnTemp.execute (InputQuery) HOWMANYFIELDS = RSTEMP.FIELDS.COUNT -1

'The number of columns in the statistics database

%>

<%

For i = 0 to hoWMANYFIELDS

%>

<% for i = 0 to hoWMANYFIELDS

Thisvalue = RSTEMP (i)

IF isnull (thisvalue) THEN

THISVALUE = "?

'If the field is empty, the value of the Variable THISVALUE is defined as a space.

END IF%>

<% = RSTEMP (i) .Name%>

<% next%>

<%

Do While Not Rstemp.eof

%>

<% = thisvalue%>

<% next%>

<% RSTEMP.MOVENEXT

LOOP%>

<%

Rstemp.close

Set Rstemp = Nothing

Conntemp.close

SET CONNTEMP = Nothingend Sub%>

Complete the Sub's definition process, in the following ASP programs, we can easily get the query results for the SQL query statement you want to use and call the process. Save the following four codes to Asp11A.asp, asp11b.asp, asp11c.asp, asp11d.asp, four .asp files.

asp11a.asp </ title> </ head></p> <p><Html> <body bgcolor = "# ffffff"></p> <p><%</p> <p>Call Query2Table ("SELECT * from Publishers Where Name Like 'a %%'")</p> <p>'Inquiry the records of all the names in the table Publishers</p> <p>%></p> <p><! - # include virtual = "/ askTEST / SUBDBTABLE.INC" -> </ body> </ html></p> <p><Head> <title> asp11b.asp </ title> </ head> <html> <body bgcolor = "# ffffff"></p> <p><%</p> <p>Call Query2Table ("SELECT * from Titles Where Year_Published> = 1998")</p> <p>'Record all the issuings in Titles greater than or equal to 1998 records</p> <p>%></p> <p><! - # include virtual = "/ askTEST / SUBDBTABLE.INC" -> </ body> </ html></p> <p><Head> <title> asp11c.asp </ title> </ head> <html> <body bgcolor = "# ffffff"> <%</p> <p>Call Query2Table ("SELECT * FROM PUBLISHERS WHERE AMOUNT> 10000 and SEX = 'MALE')</p> <p>'Inquiry all the quantity of all the quantities in the table Publishers and the sex of the male</p> <p>%></p> <p><! - # include virtual = "/ askTEST / SUBDBTABLE.INC" -> </ body> </ html></p> <p><Head> <title> asp11d.asp </ title> </ head> <html> <body bgcolor = "# ffffff"></p> <p><%</p> <p>Call Query2Table ("SELECT * from Publishers Where State <> 'NY'")</p> <p>'Inquiry all the records in the city in the city, the city is not invested in New York.</p> <p>%></p> <p><! - # include virtual = "/ askTEST / SUBDBTABLE.INC" -> </ body> </ html></p> <p>Using the defined process query2table in the subdbtable.inc file, you can query the database very quickly. What you have to do is "ConnTemp.Open" DSN = Student; Uid = Student; PWD = Aspmagic " The database name, the user identity, and the password are slightly changed, and enter the SQL query statement you want to use when calling query2table. Is it very simple? This is the charm of ASP and SQL !!!</p> <p>Today, although we use a whole piece of space only to learn a SQL instruction, please believe that you get far different from a DOS instruction, the SELECT command makes you very easy to query the database, perhaps before this There is also aware of the database query, but through this article, you have actually used the ASP to use the common database query. Is it very excited? In the next one, the author will continue to introduce some of the other SQL. Basic instructions. Before ending this article, the author will apologize to a lot of letters of friends here, because the author is busy, there is no problem, please forgive me, I will try to put some common, frequent frequency, high frequency problems in the article Write out, as for some uncommon problems, I suggest you ask questions below, you will get a timely reply. Www.onlinechina.net/friend/flybird/bbs/wwwboard.asp?id=1, this is the best Chinese ASP learning site you see so far, hosted by Shanghai's flying birds, everyone must go see.</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-58011.html</div><div class="plugin d-flex justify-content-center mt-3"></div><hr><div class="row"><div class="col-lg-12 text-muted mt-2"><i class="icon-tags mr-2"></i><span class="badge border border-secondary mr-2"><h2 class="h6 mb-0 small"><a class="text-secondary" href="tag-2.html">9cbs</a></h2></span></div></div></div></div><div class="card card-postlist border-white shadow"><div class="card-body"><div class="card-title"><div class="d-flex justify-content-between"><div><b>New Post</b>(<span class="posts">0</span>) </div><div></div></div></div><ul class="postlist list-unstyled"> </ul></div></div><div class="d-none threadlist"><input type="checkbox" name="modtid" value="58011" checked /></div></div></div></div></div><footer class="text-muted small bg-dark py-4 mt-3" id="footer"><div class="container"><div class="row"><div class="col">CopyRight © 2020 All Rights Reserved </div><div class="col text-right">Processed: <b>0.043</b>, SQL: <b>9</b></div></div></div></footer><script src="./lang/en-us/lang.js?2.2.0"></script><script src="view/js/jquery.min.js?2.2.0"></script><script src="view/js/popper.min.js?2.2.0"></script><script src="view/js/bootstrap.min.js?2.2.0"></script><script src="view/js/xiuno.js?2.2.0"></script><script src="view/js/bootstrap-plugin.js?2.2.0"></script><script src="view/js/async.min.js?2.2.0"></script><script src="view/js/form.js?2.2.0"></script><script> var debug = DEBUG = 0; var url_rewrite_on = 1; var url_path = './'; var forumarr = {"1":"Tech"}; var fid = 1; var uid = 0; var gid = 0; xn.options.water_image_url = 'view/img/water-small.png'; </script><script src="view/js/wellcms.js?2.2.0"></script><a class="scroll-to-top rounded" href="javascript:void(0);"><i class="icon-angle-up"></i></a><a class="scroll-to-bottom rounded" href="javascript:void(0);" style="display: inline;"><i class="icon-angle-down"></i></a></body></html><script> var forum_url = 'list-1.html'; var safe_token = 'UtLuhn1_2Bz1kw6BGsLJsQMIbhoh5pjsWmyLza7OnK2niaRpjZDywhZPuO59o1yM5TMoJ6ki3Y3uJrPgBhv96oMw_3D_3D'; var body = $('body'); body.on('submit', '#form', function() { var jthis = $(this); var jsubmit = jthis.find('#submit'); jthis.reset(); jsubmit.button('loading'); var postdata = jthis.serializeObject(); $.xpost(jthis.attr('action'), postdata, function(code, message) { if(code == 0) { location.reload(); } else { $.alert(message); jsubmit.button('reset'); } }); return false; }); function resize_image() { var jmessagelist = $('div.message'); var first_width = jmessagelist.width(); jmessagelist.each(function() { var jdiv = $(this); var maxwidth = jdiv.attr('isfirst') ? first_width : jdiv.width(); var jmessage_width = Math.min(jdiv.width(), maxwidth); jdiv.find('img, embed, iframe, video').each(function() { var jimg = $(this); var img_width = this.org_width; var img_height = this.org_height; if(!img_width) { var img_width = jimg.attr('width'); var img_height = jimg.attr('height'); this.org_width = img_width; this.org_height = img_height; } if(img_width > jmessage_width) { if(this.tagName == 'IMG') { jimg.width(jmessage_width); jimg.css('height', 'auto'); jimg.css('cursor', 'pointer'); jimg.on('click', function() { }); } else { jimg.width(jmessage_width); var height = (img_height / img_width) * jimg.width(); jimg.height(height); } } }); }); } function resize_table() { $('div.message').each(function() { var jdiv = $(this); jdiv.find('table').addClass('table').wrap('<div class="table-responsive"></div>'); }); } $(function() { resize_image(); resize_table(); $(window).on('resize', resize_image); }); var jmessage = $('#message'); jmessage.on('focus', function() {if(jmessage.t) { clearTimeout(jmessage.t); jmessage.t = null; } jmessage.css('height', '6rem'); }); jmessage.on('blur', function() {jmessage.t = setTimeout(function() { jmessage.css('height', '2.5rem');}, 1000); }); $('#nav li[data-active="fid-1"]').addClass('active'); </script>