简体   繁体   English

困难的 PHP/MySQL 代码

[英]Difficult PHP/MySQL code

I have 3 DB tables我有 3 个数据库表

  1. users(userid etc)用户(用户名等)
  2. friends(friendid,userid,friendsuserid)朋友(朋友ID,用户ID,朋友用户ID)
  3. updates(updateid, userid, update)更新(更新 ID,用户 ID,更新)

Basically I've coded a script that lets you update your profile with activity(like a status) and able to become friends with one another(using friends table, userid + friendsuserid = they friends)基本上,我编写了一个脚本,让您可以通过活动(如状态)更新您的个人资料,并能够彼此成为朋友(使用朋友表,用户 ID + 朋友用户 ID = 他们的朋友)

However, when it comes to the home page displaying these updates, I can display mine(user logged in) totally fine, but the friends updates dont show.但是,当涉及到显示这些更新的主页时,我可以完全显示我的(用户登录),但朋友的更新不显示。

I'm having problems with it as it seems a lot of querys need to happen, to select the user logged in the friends table and get their friends userid... then its another select query to select all the statuses with the user logged in id and the friends id.我遇到了问题,因为似乎需要进行很多查询,选择登录到朋友表的用户并获取他们的朋友用户 ID...然后是另一个选择查询以选择用户登录的所有状态id 和朋友 id。 then another select query to select the information from all the users profile to display their name alongside the status.然后另一个选择查询从所有用户配置文件中选择信息,以在状态旁边显示他们的姓名。

At the moment, I can only think of looping the query and putting id's in an array, then putting the array into a select query for the friends updates and displaying them.. is there a better way?目前,我只能想到循环查询并将 id 放入数组中,然后将数组放入选择查询中以供朋友更新并显示它们..有更好的方法吗? code is a mess lol and im getting confused with the amount of querys(basically 2 of each for the friends and for the user logged in) must be an easier way lol代码一团糟,哈哈,我对查询的数量感到困惑(对于朋友和登录的用户,基本上每个查询 2 个)必须是一种更简单的方法,哈哈

I think you should look into JOINs and subqueries.我认为您应该研究 JOIN 和子查询。 You should make a "subquery" - select the user that is logged in then join on the friends table (to get all of the friends userIds) and then use that list to filter in updates table您应该进行“子查询” - 选择登录的用户然后加入朋友表(以获取所有朋友的用户 ID),然后使用该列表在更新表中进行过滤

basic joins explanation 基本连接解释

subquery info子查询信息

You can get updates of user and updates of user's friends by doing something similar to this.您可以通过执行类似的操作来获取用户的更新和用户朋友的更新。

SELECT u.*
FROM updates u INNER JOIN 
   (SELECT friendsuserid as userid FROM friends WHERE userid = [youruserid]
    UNION
    SELECT userid FROM friends WHERE friendsuserid = [youruserid]
   )f
ON u.userid = f.userid

Tin Tran,锡川,

yes all I can seem to get is this是的,我似乎只能得到这个

select u.* from statusupdates u INNER JOIN (select friendsuserid from friends where userid = '2' UNION SELECT userid from friends where userid = '2')f on u.userid = f.friendsuserid;SELECT u.userid,u.email,u.name FROM users u INNER JOIN (SELECT friendsuserid FROM friends WHERE userid = '2' UNION SELECT userid FROM friends WHERE userid = '2')f ON u.userid = f.friendsuserid;

however am i right in thinking that is in fact two querys?但是我认为这实际上是两个查询是否正确? That in a mysql command line(testing querys to see if they give me what i need before i code it in) gives me everything i need.在 mysql 命令行中(测试查询以查看它们是否在我编码之前给了我需要的东西)给了我我需要的一切。 However pretty sure mysql wont accept that in a php code as it's two querys.但是很确定 mysql 不会在 php 代码中接受它,因为它是两个查询。 I've tried using inner join to add that users table to it but it doesn't seem to be correct我试过使用内连接将用户表添加到它,但它似乎不正确

EDIT:编辑:

After messing around in command line i managed to get what i need在命令行中搞砸之后,我设法得到了我需要的东西

SELECT s.status,u.name,f.friendsuserid FROM statusupdates s JOIN users u ON s.userid=u.userid JOIN friends f ON u.userid=f.friendsuserid; SELECT s.status,u.name,f.friendsuserid FROM statusupdates s 加入用户 u ON s.userid=u.userid 加入朋友 f ON u.userid=f.friendsuserid;

However, that only gives me my friends updates, not mine.然而,这只给我我的朋友更新,而不是我的。 I've tried putting a where in but it doesnt work.. I'm able to pull everything i need now, except my own updates and will it only display $_SESSION['userid'] statuses and their friends?我试过把一个 where 放进去,但它不起作用.. 我现在可以拉出我需要的一切,除了我自己的更新,它只会显示 $_SESSION['userid'] 状态和他们的朋友吗?

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

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