简体   繁体   English

将两个查询合并在一起

[英]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: Oke我的网站使用Facebook Api,它将导入将要登录的用户的所有朋友(userId,name),因此我的表结构如下:

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 我们与用户John一起登录,我们正在搜索迈克尔

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: 编辑:所以你需要一个按名称搜索朋友的查询,并检查朋友是否存在于users表中,如下所示:

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: 以下是查询他的名字like "%Michael%"查询,并检查他是否是注册用户:

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) . 花一些时间学习http://en.wikipedia.org/wiki/Join_(SQL) It is very useful. 这非常有用。

You could use a JOIN for that. 你可以使用JOIN。 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 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. 理想情况下,您的usersFriends表和users表之间会有一个外部约束。 This ensures that there will always be a corresponding users record. 这可确保始终存在相应的users记录。 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: 要设置外键,您需要确保您的表都是InnoDB并添加约束,然后您可以使用:

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 = ?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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