简体   繁体   English

两个表的联接问题

[英]Join issue with Two tables

Hi I have two tables one is called project_table and another one called investment table. 嗨,我有两个表,一个叫project_table,另一个叫投资表。

project_table: project_table:

project_id               
project_name  
project_location         
project_phase        
project_capital      
project_total 
project_notes 
project_file 

investment table: 投资表:

id  
project_id 
investor_id     
investment 

user table: 用户表:

user_id
user_name
user_email
user_pass
user_role
user_phone 

I'm storing the investment from user in a table (investment) and linking that table to project table using project_id 我将来自用户的投资存储在一个表中(投资),并使用project_id将该表链接到项目表

What i need: Display Invested project data from a user. 我需要什么:显示用户投资的项目数据。 Means if a user login to admin they only need see they project which they have invested, Don't need to display all projects. 意味着如果用户以管理员身份登录时只需要查看他们已投资的项目,则无需显示所有项目。 A user may have option to invest in multiple projects. 用户可以选择投资多个项目。 So basically if James login to admin he only need see project which have investment from James. 因此,基本上,如果James登录到管理员,他只需要查看由James投资的项目即可。

My function for this is: 我的功能是:

public function list_all_projects_by_userid ($uid,$investor){

    $investors = DB::fetch('SELECT * FROM  project_table JOIN investment on project_table.project_id = investment.project_id;  ORDER BY project_id DESC');
   return $investors;   


        } 

Buts its not working, Please help me. 但是它不起作用,请帮助我。 i am using a Extended PDO class which have a fetch method. 我正在使用具有访存方法的扩展PDO类。

Have a look at this! 看看这个!

SELECT * FROM  project_table t JOIN investment i on t.project_id = i.project_id where t.project_name='john' ORDER BY project_id DESC;

You placed a ";" 您放置了“;” before ORDER BY which is wrong. 在ORDER BY之前,这是错误的。 I guess that the above query would do the trick. 我想上面的查询可以解决问题。

you have to pass the 你必须通过

project_name='john' PROJECT_NAME = '约翰'

as per your requirement. 根据您的要求。

for that you need to make connection between usertable and project_table. 为此,您需要在usertable和project_table之间建立连接。

There is a syntax error in your query. 您的查询中存在语法错误。 Pl try this: 请尝试一下:

SELECT * FROM  project_table JOIN investment on project_table.project_id = investment.project_id  ORDER BY project_id DESC;

If your Join criteria is not meeting, pl explain your functionality in detail. 如果您的加入条件不符合,请详细说明您的功能。

You need to put the ';' 您需要输入“;” behind the whole query: 在整个查询之后:

SELECT *
FROM  project_table JOIN investment
   on project_table.project_id = investment.project_id
ORDER BY project_id DESC;

since invester is an user so function doesn't need two arguments,so i kept it simple 因为投资人是用户,所以函数不需要两个参数,所以我保持简单

public function list_all_projects_by_userid ($uid){
$sql="select * from investment_table Inner Join project_table
on project_table.project_id=investment_table.project_id
where investment_table='".$uid."' ORDER BY investment_table.project_id DESC";
$investors = DB::fetch($sql);
return $investors; 
}

In your function you put semi colon before the order by. 在您的函数中,您将半冒号放在排序之前。

project_id present in both the tables. 两个表中都存在project_id。 so you should mention in which table you need to set order by for project_id. 因此,您应该提及需要在哪个表中为project_id设置顺序。

public function list_all_projects_by_userid ($uid,$investor){ 公共功能list_all_projects_by_userid($ uid,$ investor){

$investors = DB::fetch('select * from investment_table, project_table where project_table.project_id=investment_table.project_id  ORDER BY investment_table.project_id DESC');

return $investors; 返还投资者 } }

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

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