简体   繁体   English

MySQL左联接以显示一个表的值,但存储在第二个表中

[英]MySQL left join to display one table's values but store in second table

I'm building a property website and have created a page to create a new property. 我正在建立一个房地产网站,并创建了一个页面来创建一个新的房地产。 When I want to edit it, I'm having difficulty displaying desired results. 当我要编辑它时,我很难显示期望的结果。

Relevant details: Property table has a developer field which is the id from developer table. 相关详细信息:属性表具有一个开发人员字段,该字段是来自开发人员表的ID。 property create page has a drop down to select a string (taken from developer table) shown to the user but the id is stored in developer field 属性创建页面上有一个下拉列表,用于选择显示给用户的字符串(从开发人员表中获取),但ID存储在开发人员字段中

When I want to set an edit page, I need to show the current table values but allow to select as before. 当我想设置一个编辑页面时,我需要显示当前表的值,但允许像以前一样选择。

I've tried this but it doesn't show the current table's value and due to developer and id columns being used in both tables, I used "as" to create a shortened unique form. 我已经尝试过了,但是它没有显示当前表的值,并且由于在两个表中都使用了developer和id列,我使用“ as”创建了一个缩短的唯一形式。

<?php
$qry=mysql_query("SELECT Distinct a.id as a_id,a.developer as a_dev,b.developer as b_dev FROM developer  a left join property b on b.developer=a.id", $con);
if(!$qry)
{
    die("Query Failed: ". mysql_error());
}
?>
<div class="title">Select a  <?php echo $title2 ?> : </div>
<div class="description">
<select name="field2" id="field2">
<option value=""></option>
<?php
// First, define the desired default value
$default_value = $row['b_dev'];
while($row=mysql_fetch_array($qry))
{
    // Then you can mark that one as selected in your "while" loop
    $selected = ($row['b_dev'] == $default_value) ? ' selected' : '';
    echo "<option value='" . $row['a_id'] . "'" . $selected . ">" . $row['a_dev'] . "</option>";
}
?>

The selection drop down is working fine, but the currently stored field value for b_dev doesn't display. 选择下拉列表工作正常,但当前未显示b_dev的字段值。 I've tried to use variables also, but it didn't show the stored value. 我也尝试使用变量,但是没有显示存储的值。 one more caveat is that the stored value in b.developer is a number and the displayed value has to be the string that matches b.developer=a.id so we only see the string and not the integer. 还有一点需要注意的是,b.developer中存储的值是一个数字,显示的值必须是与b.developer = a.id匹配的字符串,因此我们只能看到字符串,而不是整数。

I've given relevant details, if anyone requires more details, please mention in comments, and I will try to provide. 我已经提供了相关的详细信息,如果有人需要更多详细信息,请在评论中提及,我会尽力提供。

PS: Yes I know I should be using MySQLi or PDO, but I will convert it once I get the structure and back end completed. PS:是的,我知道我应该使用MySQLi或PDO,但是一旦结构和后端完成,我将对其进行转换。

What is $default_value = $row['b_dev']; 什么是$default_value = $row['b_dev']; ? Specifically, what is $row in your case? 具体来说,您的情况下$row是多少? I assume $row is unassigned before your loop. 我假设$ row在循环之前未分配。

You need to set $default_value to your current value, which you need to get from the database as a separate query. 您需要将$default_value设置$default_value当前值,您需要将其作为单独的查询从数据库中获取。

Apparently I had to rewrite my sql to take in all the information at once using left joins. 显然,我必须重写我的sql,才能使用左联接同时接收所有信息。 It works on some of the stuff i tried. 它适用于我尝试过的一些东西。

$qry_main=mysql_query("SELECT a.id, name, description, b.developer as pdev, c.agent as pagent ,g.country as pcountry,f.city as pcity, d.area as parea, size, furnished, h.type as ptype, finished, bed, bath, pool, featured, price,delivery from property a left join developer b on a.developer_id=b.id left join agent c on a.agent_id=c.id left join area d on a.area_id=d.id left join type h on a.type_id=h.id left join city f on d.city_id=f.id left join country g on f.country_id=g.id where a.id='$id'",$con);

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

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