There are two tables: user, proc_leader .

The first table consists of the names:

  uid| username| salary| ---+---------+-------+ 1 | beny | 0 | 2 | barikan | 0 | 

The second proc_leader table consists of foreign key :

  projectNo| process | proc_leader| ---------+---------+------------+ 1610004 | ANM BLD | beny | 1610004 | BGD CUP | barikan | 1610005 | ANM BLD | bob | 1610006 | BGD CUP | barikan | 

All data in this table are foreign key to other tables:

  • projectNo foreign key in the project table
  • process foreign key in the theme_name table
  • proc_leader foreign key in the user table

I want to output using sql each name from the user table, each process and the project number to which it is assigned. For example, something like this:

  uid| username| projectNo| process| ---+---------+----------+--------+ 1 | beny | 1610004 |ANM BLD | 1 | beny | 1610004 |BGD CUP | 2 | bob | 1610005 |ANM BLD | 2 |barikan | 1610006 |BGD CUP | 

How can this be done?

  • five
    Take some kind of initial tutorial on sql, for example on sql-ex.ru. Your question relates to basic SQL knowledge, which is obtained in 1 day - Mike

2 answers 2

Apparently, the usual join'om:

 SELECT * FROM user INNER JOIN proc_leader pl ON (username = pl.proc_leader) 

And I would not use the name in proc_leader, but the uid: the names are already in user

    Use join tables :

     select u.uid, u.username, pl.projectNo, pl.process from user u join proc_leader pl on u.username = pl.proc_leader 

    An example on sqlfiddle .