简体   繁体   English

需要帮助来创建单个Mysql查询

[英]Need help creating a single Mysql query

I have a table that stores the names of people in a users circle. 我有一个表,用于存储用户圈子中的人员姓名。 Relevant fields are: username & circleMember 相关字段为:用户名和circleMember

I have another table that that stores all listings create by individual users. 我有另一个表存储由个人用户创建的所有列表。

My app sends a request to the server providing the active users username. 我的应用程序向服务器发送请求,提供活动用户用户名。

I need a query that will SELECT from circle circleMember WHERE username = ?username and then SELECT from listings * WHERE username = (the result of first query) the username, circleMember combinations are stored on individual rows so there will be multiple rows for each username. 我需要一个查询,它将从circle circleMember WHERE username =?username然后SELECT from listings * WHERE username =(第一个查询的结果)用户名,circleMember组合存储在各个行上,因此每个用户名将有多行。

table circle
 - circleId (int)
 - username (varchar) 
 - circleMember (varChar)

table listings
- listingId (int)
- username (varchar) 
- image (varchar) 
- title (varchar) 
- price (decimal 15,2) 
- description (varchar) 
- location(varchar)

My current script gets all listings from the database and presents them in Card Views. 我当前的脚本从数据库中获取所有列表,并将其显示在Card Views中。 The result I am looking for is that a user will only see listings from usernames in their circle. 我正在寻找的结果是,用户只会看到他们圈子中用户名的列表。

I have tried various combinations of single queries from posts on this site but apparently lack the proper syntax to make it work. 我已经尝试过从此站点上的帖子中对单个查询进行各种组合,但是显然缺少使它正常工作的正确语法。 Can this be done in a single query or do I need one to get results and then another to iterate through the results? 这可以在一个查询中完成,还是我需要一个获得结果然后另一个来迭代结果?

So I think you want a simple JOIN 所以我想你想要一个简单的JOIN

SELECT * FROM circle 
JOIN listings ON circle.username = listings.username 
WHERE circle.circleId = (SELECT circleId FROM circle WHERE username = <username> limit 1)

Where <username> is replaced with some quoted string. 其中<username>被替换为一些带引号的字符串。

http://dev.mysql.com/doc/refman/5.7/en/join.html http://dev.mysql.com/doc/refman/5.7/en/join.html

no clue what 'circle circleMember is supposed to be 不知道'圈圈会员应该是什么

Is this what you mean? 你是这个意思吗?

SELECT * FROM listings
JOIN circle ON circle.circleMember = listings.username 
WHERE circle.username = "my_user_name"

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

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