php MySQLI 内连接 2 个表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18288218/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:18:00  来源:igfitidea点击:

MySQLI Inner Join 2 Tables

phpmysql

提问by user1772571

What am I doing wrong here. I have followed many examples but can't seem to get this working. I have 2 tables

我在这里做错了什么。我已经遵循了许多示例,但似乎无法使其正常工作。我有2张桌子

Table => users

表 => 用户

user_id
user_name
user_email
user_password
user_country
user_dobdate
user_company
user_code
user_status
user_type

Table => applications

表 => 应用程序

apply_id
apply_from
apply_leave_type
apply_priority
apply_start_date
apply_end_date
apply_halfday
apply_contact
apply_reason
apply_status
apply_comment
apply_dated
apply_action_date

SQLI QUERY

SQLI查询

$query = $db->select("SELECT users.user_id, app.apply_from FROM users INNER JOIN applications ON  users.user_id = app.apply_from WHERE users.user_code='1'");
$rows = $db->rows();
foreach ($rows as $apply){
$apply_id = $apply['apply_id'];
$apply_from = $apply['apply_from'];

Error Message

错误信息

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in xxxxxxxxxxxxxxx line 26

采纳答案by Joachim Isaksson

Your query;

您的查询;

SELECT users.user_id, app.apply_from 
FROM users 
INNER JOIN applications 
  ON  users.user_id = app.apply_from 
WHERE users.user_code='1'

...uses an alias appfor the table application, but does not declare it.

...app为 table使用别名application,但不声明它。

INNER JOIN applications app

回答by Bharathi

You have missed the alias name for table applicationsas appin join. Try the following:

你已经错过了表的别名applicationsappjoin。请尝试以下操作:

SELECT users.user_id,app.apply_from 
FROM users 
INNER JOIN applications app ON users.user_id = app.apply_from 
WHERE users.user_code='1'

回答by user4035

Put abbreviation 'app' for applicationstable:

将缩写“app”放在applications表中:

SELECT 
    users.user_id, 
    app.apply_from 
FROM
    users 
INNER JOIN
    applications AS app
ON
    users.user_id = app.apply_from
WHERE
    users.user_code='1'