我有三张桌子
table_users: id_user | name
table_posts: id_post | id_user | post
table_follows: id_follow | id_follower | id_followed
第一个表显示了用户,第二个表显示了他们发布的帖子&第三,用户之间的关系(跟随者/跟随者)
因此,例如,如果我有用户Mattew id=3
,我想要检索他所关注的所有人“发布”的帖子。在这种情况下,亚当id=1
,汤姆id=7
& Zoe id=9
之后是Mattew。
我已经开发了一个有效的php/mysql
代码,但我想仅使用一个SQL
语句来改进它。
//retrieve all the people followed by Mattew
$followeds=mysql_query("SELECT * FROM table_follows WHERE id_follower='3' /*Mattew*/");
//create an array with the ids
$array_followeds=array();
while($fwd=mysql_fetch_array($followeds)){
$id_followed=$fwd['id_followed'];
$array_followeds[]="id_user='".$id_followed."'";
}
//if exists people followed by Mattew
if(count($array_followeds)>0){
$array_followeds=implode(' OR ', $array_followeds);
}else{/*$array_followeds="id_user=0";*/}
//main query: shows all the posts of the people Mattew follows
$main_query=mysql_query("SELECT * FROM $table_posts WHERE ($array_followeds) AND id_user!='3'/*Mattew*/");
while($posts=mysql_fetch_array($main_query)){
/*results */
}
我想建议如何使用单个语句改进Sql
查询并避免使用数组。
答案 0 :(得分:1)
选择您需要的字段
defmodule Chatroom.LobbyChannel do
require Runner
use Phoenix.Channel
{:ok, pid: spawn(fn -> Runner.input() end)}
avatar = %{x: 0, y: 0}
def join("lobby", _payload, socket) do
{:ok, socket}
end
def handle_in("new_message", payload, socket) do
case payload["message"] do
"hello" -> broadcast! socket, "new_message", payload
"jump" -> send pid, {:jump, self, [avatar: avatar, socket: socket]}
_ -> nil
end
{:noreply, socket}
end
def handle_reply do
receive do
{:result, mover, socket} -> broadcast! socket, "new_message", [name: "State", message: "5"]
end
end
end