简体   繁体   English

从PHP中的外键引用获取列值(名称)

[英]Get column value (name) from a Foreign Key reference in PHP

I've got two databases fruit and fruit-prices (for arguement's sake). 我有两个数据库“ fruit和“ fruit-prices (出于争论的目的)。

In fruit there are two columns id | name fruit ,有两列id | name id | name

Fruit table 水果桌

 id | name 
 1  | Apple 
 2  | Banana

In fruit_prices there are three columns id | fruit_id | price fruit_prices ,有三列id | fruit_id | price id | fruit_id | price id | fruit_id | price where fruit_id is a FOREIGN KEY reference. id | fruit_id | price ,其中fruit_id是外FOREIGN KEY参考。

 id | fruit_id | price
 1  |    1     | £2.00
 2  |    2     | £3.00

Now I have a PHP function that will print out a table row and cells with the information from the database but currently if I am printing two fruits out my table looks like this. 现在,我有了一个PHP函数,该函数将打印出表行和带有数据库信息的单元格,但是当前如果我要打印两个水果,则表看起来像这样。

 Name | Price 
 1    | £2.00
 2    | £3.00 

PHP Code: PHP代码:

$query = mysqli_query($conn, "SELECT * FROM fruits ");

while($row = mysqli_fetch_assoc($query)) {

    $name = $row['fruit_id']; //for comma separation
    $price = $row['price'];
    echo "<tr>";

        echo "<td>"     .$name.     "</td>" . 
             "<td>"     .$price.    "</td>";

    echo "</tr>";
}

Is there an elegant way I can retrieve the name of the fruit (ie 1 = Apple, 2 = Banana). 有没有一种优雅的方法可以检索水果的名称(即1 =苹果,2 =香蕉)。 Rather than using the unique ID of each fruit. 而不是使用每个水果的唯一ID。

So then my table will look like this 所以我的桌子会像这样

Name  | Price
Apple | £2.00

...

I hope this makes sense? 我希望这是有道理的? I'm new to RD concepts. 我是RD概念的新手。 This is a very simple example and does not reflect my entire project so I'm just wondering if this is achievable? 这是一个非常简单的示例,不能反映我的整个项目,所以我只是想知道这是否可以实现?

This can be done by joining both the tables.Change your sql query with below one.I think it will solve your problem. 这可以通过连接两个表来完成。将您的sql查询更改为下面的一个。我认为这将解决您的问题。

"SELECT fruit . name , fruit_prices . price FROM fruit , fruit_prices WHERE fruit_prices . fruit_id = fruit . id "; “选择fruitnamefruit_pricespricefruitfruit_prices WHERE fruit_pricesfruit_id = fruitid ”;

You would want to use a join and specify the columns you would like to select. 您将要使用联接并指定要选择的列。

SELECT f.name, fp.price FROM fruit as f
JOIN fruit-prices as fp ON f.id=fp.fruit_id;

It is achievable using JOIN : 使用JOIN可以实现:

SELECT * FROM fruits a JOIN fruits-prices b ON b.fruit_id = a.id

For more friendly column names you can add column aliases and use them further. 对于更友好的列名,您可以添加列别名并进一步使用它们。

You say they are in two databases, but I think it might just be two tables. 您说它们在两个数据库中,但我认为可能只是两个表。 If so: 如果是这样的话:

select * from fruit f
left outer join fruit-prices fp
on f.id = fp.fruit_id

The left outer join ensures that if a fruit doesn't have a price it will be returned will null as the price. 左外部联接可确保如果水果没有价格,则返回该价格将为null。 If you don't want that replace it with an inner join. 如果您不希望使用内部联接替换它。

Use of JOIN does what you need. 使用JOIN您的需求。

"SELECT `p`.`price`, `f`.`name` FROM `fruit_prices` `p`
JOIN `fruits` `f` ON `f`.`id` = `p`.`fruit_id`"

Be wary - use of SELECT * here will lead to an error, as both tables have an id field, and the query will break. 请注意-在这里使用SELECT *会导致错误,因为两个表都具有id字段,查询将中断。

You need a join between the tables, 您需要在表之间进行联接,

SELECT f.name, fp.price
  FROM fruit f
 INNER JOIN fruit_price fp ON f.id = fp.fruit_id

Well, you need to take a look to SQL Joins 好吧,您需要看一下SQL Joins

query = mysqli_query($conn, "SELECT FRUIT.name,FRUIT_PRICES.price FROM fruits INNER JOIN FRUIT_PRICES ON FRUIT.id = FRUIT_PRICES.fruit.id");

while($row = mysqli_fetch_assoc($query)) {

$name = $row['name']; //for comma separation
$price = $row['price'];
echo "<tr>";

    echo "<td>"     .$name.     "</td>" . 
         "<td>"     .$price.    "</td>";

echo "</tr>";
}

What I wrote means : "Select FRUIT.Name,FRUIT_PRICES.price in FRUIT AND FRUIT_PRICES, considering that FRUITS.id is related to FRUIT_PRICES.id_fruit" 我写的意思是:“考虑到FRUITS.id与FRUIT_PRICES.id_fruit相关,请在FRUIT和FRUIT_PRICES中选择FRUIT.Name,FRUIT_PRICES.price”

Suggestion : You can have a single table viz fruits with 3 columns id, fruit_name, price. 建议:您可以使用一个具有3列id,fruit_name,price的水果水果单表。

for your table structure : 对于您的表结构:

SELECT fruits.id, name, price 
FROM fruits, fruit-prices 
WHERE fruit_id=fruits.id;

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

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