
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
How to get username using ID from another table in MySQL database?
To get username using ID from two tables, you need to use JOIN and join the tables.
Let us create a table −
Example
mysql> create table demo77 -> ( -> userid int not null primary key, -> username varchar(20) -> ); Query OK, 0 rows affected (2.63
Insert some records into the table with the help of insert command −
Example
mysql> insert into demo77 values(1,'John'); Query OK, 1 row affected (0.19 mysql> insert into demo77 values(2,'Bob'); Query OK, 1 row affected (0.36
Display records from the table using select statement −
Example
mysql> select *from demo77;
This will produce the following output −
Output
+--------+----------+
| userid | username |+--------+----------+
| 1 | John || 2 | Bob |
| 3 | Mike |+--------+----------+
Following is the query to create second table −
Example
mysql> create table demo78 -> ( -> id int not null primary key, -> countryname varchar(20), -> constraint fk_id foreign key(id) references demo77(userid) -> ); Query OK, 0 rows affected (0.75
Insert some records into the second table with the help of insert command −
Example
mysql> insert into demo78 values(1,'US'); Query OK, 1 row affected (0.14 mysql> insert into demo78 values(2,'AUS'); Query OK, 1 row affected (0.15
Display records from the second table using select statement −
Example
mysql> select *from demo78 -> ;
This will produce the following output −
Output
+----+-------------+
| id | countryname |+----+-------------+
| 1 | US || 2 | AUS |
+----+-------------+2 rows in set (0.00 sec)
Following is the query to get username by using id by joining both the tables −
Example
mysql> select username from demo77 -> join demo78 -> on demo77.userid=demo78.id;
This will produce the following output −
Output
+----------+
| username |+----------+
| John || Bob |
+----------+2 rows in set (0.05 sec)
- Related Articles
- How to copy a table from one MySQL database to another?
- How to get the id after INSERT into MySQL database using Python?
- Extract the user ID from the username only in MySQL?
- How to get all table names from a database using JDBC?
- Select some data from a database table and insert into another table in the same database with MySQL
- How to Reset MySQL AutoIncrement using a MAX value from another table?
- How to retrieve table names from a database in MySQL?
- How do I get the id after INSERT into MySQL database in Python?
- Get the last record from a table in MySQL database with Java?
- Delete more than one rows from a table using id in MySQL?
- How to add a row to a table using only strings from another table as reference in MySQL?
- How to copy rows from one table to another in MySQL?
- How to get the data associated with the maximum id in a MySQL table?
- How to get the table definition in a database from AWS Glue Data Catalog using Boto3
- Get rows that have common value from the same table with different id in MySQL
