【问题标题】:SQL query to return data from bridging table returning error (likely due to my query syntax!)SQL查询从桥接表返回数据返回错误(可能是由于我的查询语法!)
【发布时间】:2019-10-13 00:52:37
【问题描述】:

我的 SQL 查询出现问题,无法使用 python/flask 从我的数据库中获取任何数据(可能是因为我对 MySQL 比较陌生,而且我的查询语法可能很糟糕!)。

我正在尝试从数据库中的桥接表中获取数据,因为它是在下面创建的:

CREATE TABLE student (
student_id int AUTO_INCREMENT,
first_name varchar (20) NOT NULL,
last_name varchar (20) NOT NULL,
year_began int NOT NULL,
PRIMARY KEY (student_id)
);

CREATE TABLE courses (
course_code varchar (7) NOT NULL,
course_title varchar (100) NOT NULL,
year_delivered int NOT NULL,
PRIMARY KEY (course_code)
);

CREATE TABLE enrollments (
student_id int NOT NULL,
course_code varchar (7) NOT NULL,
CONSTRAINT enrollment PRIMARY KEY (student_id, course_code),
FOREIGN KEY (student_id) REFERENCES student (student_id),
FOREIGN KEY (course_code) REFERENCES courses (course_code) 
);

我当前的python代码包括我的查询当前是这样写的:


        student_name = str(request.form['student_name_search'])
        student_name = ' '.join(student_name.split())

        try:
            cursor = mydb.cursor()
            cursor.execute("SELECT student_id, first_name, last_name FROM student AND course_code FROM courses\
            LEFT JOIN enrollments on student_id=student_id LEFT JOIN course_code=course_code WHERE\
                (CONCAT(first_name,last_name ) LIKE '%{}%')".format(student_name))
            search_result = cursor.fetchall()
            cursor.close()
            return render_template('studentselect_results.html', student_name = student_name)


        except mysql.connector.Error as err:
            cursor.close()
            return failure('studentsearch',f"Error message: {err}. Search didn't work")

输入搜索条件的 HTML 如下:

<form action="{{ url_for('process_searchstudent') }}" method="POST">
            <div class="form-row justify-content-center">
                <div class="form-row">
                    <div class="form-group col-12">
                        <input type="text" class="form-control" id="student_name_search" name="student_name_search" 
                        placeholder="Enter Students Name">
                    </div> 
                </div>
            </div>   
            <!-- SUMBIT/RESET BUTTONS-->
            <div class="form-row justify-content-center">
                <div class="form-group row">
                    <div class="form-group">
                        <input type="submit" class="btn btn-outline-success" value="Search">
                        <input type="button" id="resetBtn" class="btn btn-outline-danger" value="Reset">
                    </div>
                </div>
            </div>
        </form>

我希望查询返回数据的 HTML 如下:

<table class="table table-sm table-striped">
        <thread class="thread-light">
            <tr>
                <th>Student ID:</th>
                <th>Student First Name:</th>
                <th>Student Last Name:</th>
                <th>Click to View Student Details</th>
            </tr>
        </thread>
        <tbody>
            {% for each_result in search_results %}
            <tr>
                <td>{{item[0]}}</td>
                <td>{{item[1]}}</td>
                <td>{{item[2]}}</td>
            </tr>
            {% endfor %}
        </tbody>
    </table>

当在第一个 HTML 页面上单击“搜索”按钮时,会出现以下错误:

错误消息:1064 (42000):您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以获取正确的语法,以便在第 1 行的“AND course_code FROM course LEFT JOIN registrations on student_id=stud”附近使用。搜索不起作用

非常感谢任何和所有帮助!

【问题讨论】:

    标签: python mysql database flask


    【解决方案1】:

    这不是 JOIN 查询的正确语法。试试这个:

    SELECT s.student_id, s.first_name, s.last_name , c.course_code
    FROM student s
    LEFT JOIN enrollments e on e.student_id = s.student_id
    JOIN courses c ON c.course_code = e.course_code 
    WHERE CONCAT(s.first_name, s.last_name) LIKE '%{}%'
    

    【讨论】:

    • 感谢@Nick,但我希望将数据放入的 HTML 表返回空。任何想法为什么它不返回任何东西??
    • @halma562 你的LIKE 子句看起来很奇怪——{} 的值是什么?
    • 这应该在查询末尾填写.format(student_name)),其中student_name在python脚本的顶部创建student_name = str(request.form['student_name_search']) student_name = ' '.join(student_name.split())
    • 有没有办法检查查询是否返回了任何结果(我假设它没有出错,因为你有一个except 块)
    • 如果我直接在 MySQL 中运行查询并将{} 替换为字符串,例如"cor",它会毫无问题地运行查询并返回预期结果
    猜你喜欢
    • 2014-09-09
    • 2013-10-01
    • 1970-01-01
    • 2020-02-06
    • 1970-01-01
    • 2017-06-10
    • 2014-02-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多