我有一个名为 Listings
的表和一个名为 wines
的表,还有一个名为 wineinfo
我最初只使用以下方法从 listings
表中获取信息,但是自从我重组我的数据库后,它需要使用另外两个表。
$listing = $this->db->get_where( "listings", [ "listingID" => $id ]
)->row();
if( !$listing )
throw new Exception("Error: Listing you're trying to bid on does not exist.", 1);
// check not to bid on own listings
if( $listing->list_uID == $userid )
throw new Exception("Error: Dont't bid on your own listings.", 1);
然后我尝试更改代码以便 JOIN 语句可以工作
$this->db->select("FROM listings.*, Vintage, Vineyard, Wine_Name, Region, Advice, Grape,Producer,Type id,wine_id,Wine_Name,");
$this->db->from("wineinfo");
$this->db->where(["listingsID" => $id]);
$this->db->where(["wineinfo.wine_id" => "listings.wine_id"]);
$this->db->where(["wineinfo.Vintage" => "listings.wine_id"]);
$this->db->join("wines", "wineinfo.wine_id = wines.wine_id");
$listing = $this->db->get()->row();
我收到此错误。
未知表“列表”
但是 100% 有一个名为 listings 的表。
我知道我遗漏了一些东西,或者肯定是搞砸了代码,我只是刚刚了解这个,上面的代码适用于其他东西,但现在我已经修改了它,它没有。
最佳答案
I then tried changing the code so the JOIN statements could work
您正在尝试将 3 个表与 2 个 FROM
和一个 JOIN
子句结合起来,但您这样做的方式不正确。
您需要保持 SELECT
干净,只需选择您需要的列,例如:
$this->db->select("listings.*, wineinfo.*, wine.*");
然后是 FROM
子句:
$this->db->from("wineinfo");
然后进行连接:
$this->db->join("listings", "wineinfo.wine_id = listings.listingsID");
$this->db->join("wines", "wineinfo.wine_id = wines.wine_id");
后跟您的 where 子句。
请注意,我不知道你的表结构,所以我只能猜测你的JOIN
关系。这也是一个简化的示例,我假设这 3 个表没有匹配的列名。
对“模棱两可”的评论的回应:您可以将您的选择子句限制在必要的列中,例如
$this->db->select("listings.*, wineinfo.vintage, wine.*");
或使用别名:
$this->db->select("listings.*, wineinfo.wine_id as my_wineID, wine.*");
关于php - 加入 get_where - 这可能吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47971559/