简体   繁体   中英

Merge two queries together

Oke my website works with Facebook Api, it will import all the friends (userId, name) of the user that will be logged in so my table structure is as following:

users: id, name
usersFriends: id, friendUserId, name

So when the users join the website, they will get all the friends imported, example data:

user: 237374734 (facebook user id), John brouwers (name of the user)

then it will import all the friends of John. for example:

userFriends: 237374734 (John user id), 2737373, Michael jackson

Now what I need is a query that will SEARCH (by name) through the friends, which is no problem with just a join, but what I need is in the query I need to CHECK whether the user is on my website so lets take an example:

We logged in with the user John, and we are searching for michael so that would be

SELECT name FROM userFriends WHERE name LIKE %michael%

But what I need is NOT only return the name of the user, I also need a CHECK whether the user exists in the users table.

Sorry for the long story, hope that my question is clear. Thanks already!

Edit: So you need a query that search friends by Name and check whether the friend exists in the users table or not, As follows:

create table Users(
   Id int not null,
   primary key (Id),
   Name varchar(50) not null
)TYPE=MyISAM;
create table UserFriends(
   Id int not null,
   foreign key (Id) references Users(Id),
   FriendUserId int not null,
   Name varchar(50) not null
)Type=MyISAM;
insert into Users(Id,Name) values(237374734,'John brouwers');
insert into UserFriends(Id,FriendUserId,Name)
values(237374734, 2737373, 'Michael jackson');

Here is the query that will search for the suer that his name like "%Michael%" and check if he is a registered user or not:

select s.Id, s.Name, 
       case when exists 
       ( 
           select Name from users where Name like "%Michael%" 
       ) Then 'Registered User'
       else
       (
           'Not Registered'
       )
       end
       as 'Is Registered in My Website'
from 
(
      select uf.FriendUserId Id, uf.Name
      from UserFriends uf
      left join users u on u.Id = uf.friendUserId
      where uf.name like "%Michael%"
) s

This will gives you:

Id              Name        Is Registered in My Website
2737373   Michael jackson        Not Registered

Hope this will be helpful and if there is any problems don't hesitate to ask.

Select users.id
from users join usersFriends on users.id = usersFriends.friendUserId
where usersFriends.userId = ?
select u.userId 
from users u, usersFriends uf
where u.userId=uf.friendUserId and
u.userId=?

Take sometime to study http://en.wikipedia.org/wiki/Join_(SQL) . It is very useful.

You could use a JOIN for that. Consider the following sample query:

SELECT u.id, u.name FROM users u, usersFriends uf
WHERE uf.friendUserId = u.id
AND uf.userId = ?

A query like this should work, please mind that as we don't know the exact field names you have, you'd need to make some adjustments.

UPDATE

To show you that it DOES work:

create table users (id int, name varchar(100));
create table usersFriends (friendUserId int, userId int);
insert into users values (1, 'name 1'), (2, 'name 2'), (3, 'name 3');
insert into usersFriends values (1, 7), (5, 7);


mysql> SELECT u.id, u.name FROM users u, usersFriends uf
    -> WHERE uf.friendUserId = u.id
    -> AND uf.userId =7;
+------+--------+
| id   | name   |
+------+--------+
|    1 | name 1 |
+------+--------+

Ideally, you would have a foreign constraint between your usersFriends table and the users table. This ensures that there will always be a corresponding users record. To set up a foreign key you will need to ensure that your tables are both InnoDB and to add the constraint, you could then use:

ALTER TABLE usersFriends ADD FOREIGN KEY (friendUserId) REFERENCES users (id);

Otherwise, you can use a join

SELECT *
FROM usersFriends f
    JOIN users u on f.friendUserId = u.ID
WHERE f.userId = ?

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