mysql允许连接表为空_mysql – 选择一个表中的所有项并与另一个表连接,允许空值...

你必须使用左连接而不是右连接

不同的联接

内连接:仅保留两个表中有数据的行

left join:保留左表的所有行,并添加正确的行

右连接:保留右表的所有行,并添加左边的可能

左表总是我们已有的表,右表是我们加入的表.

对于记录,还有一个交叉连接,它将左表中的每一行与右表中的每一行连接起来,但是这一行并不经常使用.

我希望现在这一切对你来说更清楚:)

更正了查询

select bird_name, member_id

from birds

left join bird_likes on birds.bird_id = bird_likes.bird_id

where member_id = 2;

请注意,这假设列member_id在bird表中,否则您可以保持这样的条件:

select bird_name, member_id

from birds

left join bird_likes on

birds.bird_id = bird_likes.bird_id and

bird_likes.member_id = 2;

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我理解了你的问题。首先,需要安装Flask和MySQL驱动程序以连接MySQL数据库。你可以使用以下命令安装它们: ``` pip install Flask pip install pymysql ``` 接下来,你需要编写一个Flask应用程序来查询MySQL数据库中的数据并将其显示在show.html页面上。这里是一个示例应用程序: ```python from flask import Flask, render_template import pymysql app = Flask(__name__) @app.route('/') def show(): # 连接MySQL数据库 conn = pymysql.connect(host='localhost', port=3306, user='root', password='password', db='database_name') cursor = conn.cursor() # 查询status表中所有数据 cursor.execute('SELECT * FROM status') data = cursor.fetchall() # 关闭数据库连接 cursor.close() conn.close() # 渲染show.html模板并传递数据 return render_template('show.html', data=data) if __name__ == '__main__': app.run() ``` 在这个应用程序中,我们使用了Flask的`render_template`函数来渲染show.html模板,并传递了从MySQL数据库查询到的数据。现在,我们需要在show.html模板中显示这些数据,并添加一个按钮来更新is_show字段。 下面是show.html模板的示例代码: ```html <!DOCTYPE html> <html> <head> <title>MySQL Status</title> </head> <body> <table border="1"> <tr> <th>ID</th> <th>Name</th> <th>Status</th> <th>Action</th> </tr> {% for row in data %} <tr> <td>{{ row[0] }}</td> <td>{{ row[1] }}</td> <td>{{ row[2] }}</td> <td><button onclick="updateStatus({{ row[0] }}, {{ row[2] }})">Update</button></td> </tr> {% endfor %} </table> <script type="text/javascript"> function updateStatus(id, status) { // 发送POST请求更新is_show字段 var xhr = new XMLHttpRequest(); xhr.open('POST', '/update', true); xhr.setRequestHeader('Content-Type', 'application/json;charset=UTF-8'); xhr.onreadystatechange = function() { if (xhr.readyState == 4 && xhr.status == 200) { alert(xhr.responseText); } }; xhr.send(JSON.stringify({'id': id, 'status': status})); } </script> </body> </html> ``` 在这个模板中,我们使用了Flask的模板引擎来循环遍历从服务器传递过来的数据,并将它们显示在HTML格中。还添加了一个按钮来更新is_show字段。当用户点击按钮时,会触发JavaScript函数`updateStatus`,该函数发送一个POST请求到服务器以更新is_show字段。 现在,我们需要添加一个新的路由来处理POST请求并更新MySQL数据库中的数据。下面是更新数据的示例代码: ```python @app.route('/update', methods=['POST']) def update(): # 获取POST请求中传递的数据 id = request.json['id'] status = request.json['status'] # 连接MySQL数据库 conn = pymysql.connect(host='localhost', port=3306, user='root', password='password', db='database_name') cursor = conn.cursor() # 更新is_show字段 new_status = 0 if status == 1 else 1 cursor.execute('UPDATE status SET is_show=%s WHERE id=%s', (new_status, id)) conn.commit() # 关闭数据库连接 cursor.close() conn.close() # 返回更新成功的消息 return 'Status updated successfully!' ``` 在这个路由中,我们首先获取POST请求中传递的数据,然后连接MySQL数据库并更新is_show字段。最后,返回一个成功的消息。 现在,你可以启动这个应用程序并在浏览器中访问它。你将看到一个格显示MySQL数据库中的数据,并且每一行都有一个按钮来更新is_show字段。当你点击按钮时,它将发送一个POST请求到服务器并更新数据库中的数据。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值