简体   繁体   English

MySQL 从两个不同的表中获取用户 ID 和密码的查询

[英]MySQL a query to get user_id and password from two different tables

  • Professeur (professeur_id, name, password,mail)教授(professeur_id、姓名、密码、邮箱)
  • Student (student_id, name, password, mail)学生(student_id、姓名、密码、邮件)

How can I write a query to get ID and password from the two tables?如何编写查询以从两个表中获取 ID 和密码?

Also, can I make a login table that contains ID and password as a foreign key from the two tables?另外,我可以从两个表中创建一个包含 ID 和密码作为外键的登录表吗?

(Do I have to change the professeur ID and student ID columns to have the same name?) (我是否必须将教授 ID 和学生 ID 列更改为具有相同的名称?)

Use a union select:使用联合 select:

select
  p.professeur_id as id
  , p.name as name
  , p.password as password
  , p.mail as mail
  from Professeur p
union select
  s.student_id as id
  , s.name as name
  , s.password as password
  , s.mail as mail
  from Student s
  ;

Please note that at least the fields professeur_id and student_id must have an alias because of their different name.请注意,至少字段professeur_id 和student_id必须具有别名,因为它们的名称不同。 But giving all fields an alias is a good practice.但是给所有字段一个别名是一个很好的做法。

For more information (not only on select statement) please go to official MySQL documentation有关更多信息(不仅在 select 声明上),请 go 到官方 MySQL 文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 查询以从两个不同的表中获取userimage路径和用户信息 - query to get userimage path and user information from two different tables 如何从不同的MySQL表获取特定的ID - How to get specific id's from different MySQL Tables 如何获取保存在 USER_ID 中的数据? - How to get the data saved in a USER_ID? 试图从sqlite调用user_id - trying to call user_id from sqlite MySQL同时插入用户名和密码,但在不同的表中 - MySQL Insert User and Password at the same time, but in different tables MySQL-每个日期每个user_id的SELECT MIN时间 - MySQL - SELECT MIN time per user_id per date 如何从请求 header 中获取 user_id 而不是将其作为请求参数传递? 然后通过header发回 - How can I get the user_id from the request header instead of passing it as a request parameter? And then send it back through the header 从两个不同的mysql表中显示JTextfield中的数据 - Displaying data in JTextfield from two different mysql tables 数据库查询可在单个查询中从不同表中获取计数总和 - Database Query to get sum of count from different tables in a single query Spring Data JPA同时保存两个实体,“ user_id”列中为空值 - Spring Data JPA saving two entities at the same time, null value in column “user_id”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM