Click here to Skip to main content
15,885,875 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How can I fetch Username(s) from different rows of same table, given, UID of 1st person. ClientUID is the relation we have here to link both the rows.

The table is as follows.

---------------------------------------------------
UID............Username............ClientUID
1111...............Rick.....................2222
2222...............John....................3333


I want to fetch Usernames Rick and John using single SQL query and I have UserId = 1111 data with me. Currently I have broke them down to two queries, but looking for better solution. Please suggest if any.
Posted
Updated 18-Jul-15 20:59pm
v2
Comments
Kornfeld Eliyahu Peter 19-Jul-15 2:56am    
It would help to show us your current solution...

If I understand your question correctly, you want both of the users on the same row.
If this is the case you can use join. So something like
SQL
SELECT *
FROM user u1
     INNER JOIN user u2 on u1.clientuid = u2.uid
WHERE u1.uid = 1111;

On thing to consider is that if the ClientUID is not mandatory, you could use LEFT OUTER JOIN instead of INNER JOIN. Have a look at Using Outer Joins[^]

EDIT

Example how to give an alias name for a column in the result set:
SQL
SELECT u1.UID         AS UID1,
       u1.UserName    AS User1,
       u2.UID         AS UID2,
       u2.UserName    AS User2
FROM user u1
     INNER JOIN user u2 on u1.clientuid = u2.uid
WHERE u1.uid = 1111;

With the example above you can fetch the values from the reader with

  • reader["UID1"]
  • reader["User1"]
  • reader["UID2"]
  • reader["User2"]
     
    Share this answer
     
    v3
    Comments
    Jones Aaron 19-Jul-15 4:27am    
    Your solution worked. Thanks.

    However, while fetching the values from reader, I am using the hardcoded column number: reader[10] instead of the row heading (reader[UID]).

    Could you please share how to fetch because the values by passing the actual tables row header: reader["UID"] to return "John"?
    Wendelius 19-Jul-15 5:22am    
    Have a look at the modified solution
    Jones Aaron 19-Jul-15 6:09am    
    Awesome. Thanks.
    Wendelius 19-Jul-15 10:22am    
    You're welcome :)
    I'd likely use a Common Table Expression to fetch the first row, then a UNION to fetch second.

    For Example

    SQL
    WITH cte AS
    (
      SELECT * FROM table WHERE UID=@uid
    )
    SELECT * FROM cte
    UNION ALL 
    SELECT table.* FROM table INNER JOIN cte ON table.UID=cte.ClientUID


    For something more complex, you might need to use a recursive CTE.
     
    Share this answer
     
    Comments
    Jones Aaron 19-Jul-15 4:08am    
    Thanks for your suggestion.

    Following this I have parameterized queries. How do I differentiate between the first row's Username and that of 2nd row when using reader.? Please find the below code modified as suggested.

    cmdString = ("WITH cte AS (SELECT * FROM dbo.UserDetails WHERE UID = @uid) SELECT * FROM cte UNION ALL SELECT dbo.UserDetails.* FROM dbo.UserDetails INNER JOIN cte ON dbo.UserDetails.UID=cte.ClientUID");

    SqlCommand cmd = new SqlCommand(cmdString, Conn);

    cmd.Parameters.Add("uid", SqlDbType.NVarChar).Value = UID;

    try
    {
    if (Conn.State.Equals(ConnectionState.Closed))
    {
    Conn.Open();
    SqlDataReader reader = cmd.ExecuteReader();
    if (reader.Read())
    {
    Username.Text = reader["username"].ToString(); -> Rick
    ...
    ...
    ...
    ClientUsername.Text = reader["?????"].ToString(); -> Rick
    }
    Conn.Close();
    }
    PIEBALDconsult 19-Jul-15 11:03am    
    You could compare the returned UID to the one you passed in.

    This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



    CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900