#P10486. [入门赛 #25] sql
[入门赛 #25] sql
Background
SQL is a powerful database query language. In this problem, you need to implement a SQL parser with partial functionality.
Problem Description
A database can be seen as a collection of several 2D tables.
For example, a database of a student information management system may have the following two 2D tables:
basic_info:
| name | sid | grade |
|---|---|---|
| Fusu | 001 | 1 |
| Maxmilite | 002 | 2 |
| Expect2004 | 003 |
GPA:
| sid | GPA |
|---|---|
| 001 | 77.88 |
| 002 | 99.9 |
| 003 | 99.7 |
The first row of the table above is called the header. Each following row represents one record of the table. For example, the second row of basic_info means that there is a student whose name (name) is , student ID (sid) is , and grade (grade) is .
A formal SQL language should support cross-table queries, but in this problem, you only need to support queries within a single table. We assume that all attributes in the table are given as strings.
You need to support query statements in the following format:
select [columns] from [table_name] where [header]=x
In the statement above, columns is a list of headers, table_name is the table name, header is a header, and is the given condition. It means: in the table table_name, find all rows whose value in the header column equals , and output the information in the specified columns for those rows.
For example, executing the following statement on the tables in the example above:
select name from basic_info where grade=1
means: in the basic_info table, find all rows where the grade column is , and output their name column. Therefore the result is:
Fusu
If you execute:
select name,sid,grade from basic_info where grade=2
then you find all rows in the basic_info table where grade is 2, and output their name, sid, and grade in order. The result is:
Maxmilite 002 2
Expect2004 003 2
As you can see, the first parameter group columns in a select statement may contain multiple header names. In that case, you should output each corresponding column in the order given by columns. If the condition after where matches multiple rows, you should output the required information for each row in the input order of the table (from top to bottom).
You need to implement this SQL statement parser and output the query results. Note that we guarantee that there is only one table name in each query statement, and there is exactly one equality condition after where.
Input Format
The first line contains an integer , indicating the number of tables in the database.
Then the information of each table is given in order:
The first line is a string table_name, indicating the name of the table.
The second line contains two integers , indicating that the table has rows and columns.
The third line contains strings, indicating the headers of the table.
The next lines each contain strings, giving the information of each row in the table.
Next is an integer , indicating the number of SQL statements.
The next lines each contain one SQL statement, and the format is guaranteed to be:
select [columns] from [table_name] where [header]=x
It is guaranteed that there are no spaces inside the four parameters columns, table_name, header, and x.
The testdata guarantees that the query statements are valid. That is, the headers and table names provided by columns, table_name, and header all exist, and the given condition can always match at least one row, so the query result is non-empty.
It is guaranteed that there are no duplicate header names in columns.
Output Format
For each query, output several lines in order, representing the query result.
In one line of query result, if there are multiple columns to output, separate the strings with one space.
1
basic_info
4 3
name sid grade
Fusu 001 1
Maxmilite 002 2
Expect2004 003 2
2
select name from basic_info where grade=1
select sid,name from basic_info where grade=2
Fusu
002 Maxmilite
003 Expect2004
2
basic_info
4 3
name sid grade
Fusu 001 1
Maxmilite 002 2
Expect2004 003 2
GPA
2 2
sid GPA
001 77.88
1
select GPA from GPA where sid=001
77.88
2
show_corner
3 2
h1 h2
1 2
1 2
show_corner2
3 1
h0
1
1
2
select h1,h2 from show_corner where h1=1
select h0 from show_corner2 where h0=1
1 2
1 2
1
1
Hint
Explanation for Sample 3
The input tables may contain several duplicate rows. You do not need to handle these rows specially; you can treat them as different rows. Each query must output all rows that meet the condition. For example, in the first SQL statement, the row 1 2 appears twice and satisfies the condition, so 1 2 is output twice.
Constraints and Conventions
| Test Point ID | Number of tables is | Number of headers is | Only one header in columns |
|---|---|---|---|
| 1, 2 | |||
| 3, 4 | |||
| 5, 6 | |||
| 7, 8 | |||
| 9, 10 |
For all testdata, it is guaranteed that:
- .
- , .
- .
- The length of
table_namedoes not exceed . - The string lengths of the table contents and headers do not exceed , and the length of
columnsdoes not exceed . - Except for the
columnsinformation, the input strings do not contain commas,. - All input strings are visible characters, with ASCII range (inclusive), and do not contain the symbol
=.
Hint
Please note the impact of large output on program efficiency. Choose an appropriate output method to avoid timeout.
Translated by ChatGPT 5