简体   繁体   中英

Join from two tables mysql

I have a doubt where trying to join two tables by a previous search. I've looked several solutions and read some chapters in a mysql book but I think I'm pretty close to the right answer but still not get it

I have this table "userprocess":

idProcess          username         state
------------------------------------------
1                  blisssing        3
2                  enriquecalvera   1
2                  africabaluja     2
1                  enriquecalvera   3
2                  blisssing        1 

The primery key for this table is the union of idProceso+username.

I have this other table "user":

index      username        pass            active      tipeUser     .... so on
-----------------------------------------------------------------   ----
1          blisssing       6OiZVVUPi3LDE   1           user
2          carmen          6OOtfrXB2Nu5.   1           user 
3          consuelo        6OgdhVSkr1VDs   1           user
4          africabaluja    6OoPtGjWMQARE   1           user
5          enriquecalvera  6O6tvHg.122uQ   1           user

The thing is I want to show the join of the two tables but with a search within the first table. If I run this query

SELECT username FROM userprocess where idProcess='1' ORDER BY state

I get this:

username
---------
blisssing
enriquecalvera

which is what I am looking for, but I want to show all the fields in the "user" table for those usernames ordered by idProceso. So I run this other query:

SELECT *
FROM
  user u,
  userprocess p
WHERE
  u.username=p.username
  AND u.username IN (
    SELECT username 
    FROM userprocess 
    where idProcess='1'
    ORDER BY username
) ORDER BY p.state

I got this:

username        pass           active   tipeUser   idProcess    state
----------------------------------------------------------------------
blisssing       6Od3nSkfOiwlg  1        user       2            1 
enriquecalvera  6Oc9usiDEk51U  1        user       2            1
enriquecalvera  6Oc9usiDEk51U  1        user       1            3
blisssing       6Od3nSkfOiwlg  1        user       2            3

But this is not what I want I just want the same two results as in the previous query but with all the columns of the result of joining the two tables..

I know there is a lot of questions like this, but I have tried a lot of things and still not having the desire result..

What am I missing?

thank you, if you have any qestion or doubt just ask :)

The reason you're seeing multiple results is because you're joining on just the username, but of course the userprocess table has 2 rows where username = enriquecalvera . Your subquery is correctly only returning the 1 row you're interested in (where idprocess = 1 ) but as your join is seperate to this, and therefore doesn't include the idprocess = 1 condition, you're getting both rows back.

You should just do this in one step with a join like this:

SELECT *
FROM
  user u
INNER JOIN userprocess p on u.username=p.username and p.idProcess='1'
ORDER BY p.state

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM