Select a specific row with additional column that its value is from another column in one table

JON PANTAU

Suppose I have a table named users consist of columns: user_id, user_name, user_created_by.

+------------------+----------------------+-------------------+
|    user_id       +      user_name       +  user_created_by  +
+------------------+----------------------+-------------------+
|        1         |        John          |         1         |
|        2         |        Ann           |         1         |
|        3         |        Paul          |         2         |
|        4         |        King          |         2         |
|        5         |        Dirk          |         3         |
+------------------+----------------------+-------------------+

The value of user_created_by is the user_id who created that record. Now, I want to make a query that results one specific row with added column let's say user_created_by_name which is the user_name of the user_id from the user_created_by. Suppose we want to get "Paul"'s record with who (the name) create it (temporary new column). For ease of understanding this is my expected result:

+----------+--------------+-------------------+------------------------+
| user_id  |   user_name  |  user_created_by  |  user_created_by_name  |
+----------+--------------+-------------------+------------------------+
|    3     |     Paul     |        2          |          Ann           |
+----------+--------------+-------------------+------------------------+

this is my query using codeigniter:

$query=$this->db->query("SELECT *, 
                           (SELECT user_name FROM users WHERE user_id = user_created_by) 
                              AS "user_created_by_name" FROM users WHERE user_id=3);

But my result are:

+----------+--------------+-------------------+------------------------+
| user_id  |   user_name  |  user_created_by  |  user_created_by_name  |
+----------+--------------+-------------------+------------------------+
|    3     |     Paul     |        2          |         NULL           |
+----------+--------------+-------------------+------------------------+
scaisEdge

You culd use a self join (join the same table two time) using alias for fere to the tables as different sets of data

SELECT a.user_id, a.user_name, a.user_created_by, b.user_name as user_created_by_name
from users a 
inner join user b on a.user_created_by = b.user_id 
where a.user_id  = 3 

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

SQL: Select a row from a table with an additional column containing the next value of the column

Select specific row from mysql table based on a column value

SELECT an additional column from another table based on a value between 2 other values

Select rows with the same value in one column and specific values in another column

R dplyr - select values from one column based on position of a specific value in another column

Query to select value from another table as a column

SQL Select where a column contains one value but not another in another row

Mysql select dynamic row values as column name from a table and value from another table

SQL Select all columns from one table and Max value of another column on another table

Fill one Dataframe Column from specific value in list of another column

How to insert row into a table by select column name and value from another table in SQL Server

SQL select value from one column based on values in another column

Select values from one column which share a value in another column

copy one row from a table to another and insert value of one column in same query

Oracle trigger change value of column/row from one table during insert of another table

How to select the last non-null value from one column and also the value from another column on the same row in Polars?

postgres - select one specfic row of another table and store it as column

How to divide one row by another with same value in a specific column?

How to select a specific row from the table with one column as a sum of values of other rows?

for Loop for multiplying a value of each row with a specific column from another table based on condition in Python

SQL select *, but override 'column value' of row if present in another table

Excel - select value from first and last row with value in another column

TSQL - Select column values from one table to another one

Get specific value from table with table name, row and column index

Select from one table and order by column of another table

MSSQL Select column values from one table to another table

while creating a table select one column from another table sql

SQL Select Based On Max Value of One Column and Specific Id from Another

Select only one row from a DB Table depending on a column value in ABAP Open SQL