[英]Codeigniter JOIN - not a unique table/alias issue
I have bookings tables from several countries (for example: book_uk) these tables contain information about our customers in different countries such as name, address, etc etc. Then in my results table I have the results of all tests this customer has had, the information is 'linked' by a PIN number, eg 336699, this number is referenced in both the book_uk table and the results table. 我有来自几个国家/地区的预订表(例如:book_uk),这些表包含有关我们在不同国家/地区的客户的信息,例如姓名,地址等。然后在我的结果表中,我有该客户所进行的所有测试的结果,信息通过PIN码(例如336699)“链接”,在book_uk表和results表中都引用了该数字。
What I want to do is select all data from the book_uk table and then link each row to the results table (where the PIN matches) but I keep getting not a unique table/alias, here is my model code: 我想做的是从book_uk表中选择所有数据,然后将每一行链接到结果表(PIN匹配的地方),但是我一直没有得到唯一的表/别名,这是我的模型代码:
$database->select($new_country_columns)
->from($country_table);
$database->select($p_results_cols)
->from($table);
$database->join('p_results', $join, 'inner');
$database
is defined earlier on in the model $database
在模型的前面定义
Produces this error: 产生此错误:
Error Number: 1066
Not unique table/alias: 'p_results'
SELECT `book_uk`.`pin`, `book_uk`.`clinic`, `book_uk`.`dob`, `book_uk`.`gender`, `book_uk`.`country`, `book_uk`.`order_date`, `p_results`.`pin`, `p_results`.`code`, `p_results`.`name`, `p_results`.`result` FROM (`book_uk`, `p_results`) INNER JOIN `p_results` ON 'book_uk.pin = p_results.pin'
Filename: C:/xampp/htdocs/projects/b2k-stat/system/database/DB_driver.php
Line Number: 691
what I really need to do is select data from book_uk and then run a foreach loop on the results table and add any data with matching PIN to the results array, I have no idea how to do this, I tried to loop through it with a foreach loop but it kept coming up blank, i've also tried: 我真正需要做的是从book_uk中选择数据,然后在结果表上运行一个foreach循环,并将任何具有匹配PIN的数据添加到结果数组中,我不知道如何执行此操作,我试图通过一个foreach循环,但一直空白,我也尝试过:
$database->select($new_country_columns)
->from($country_table)
->join('p_results', $join, 'inner');
But this also gives me zero results, any help? 但这也给我零结果,有帮助吗? Thanks in advance
提前致谢
Remove 去掉
'p_results'
from your 从你的
FROM
clause since your joining it right after that. 条款,因为您在此之后立即加入。
Not sure if this is the best way to do things, but I solved my issue by generating 2 queries and running an IF statement inside a foreach loop on both queries like so: 不知道这是否是最好的处理方式,但是我通过生成两个查询并在两个查询的foreach循环内运行IF语句来解决了我的问题,如下所示:
$database->select($new_country_columns)
->from($country_table);
$query1 = $database->get()->result_array();
$p_results->select($p_results_cols)
->from($table);
$query2 = $p_results->get()->result_array();
foreach($query1 as $row){
foreach($query2 as $rows) {
if($row['pin'] === $rows['pin']) {
$new_row = array_merge($row, $rows);
$query[] = $new_row;
}
}
}
return $query;
This code gave me an array of the combined data that was needed to display in my view 这段代码为我提供了在我的视图中需要显示的组合数据数组
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.