简体   繁体   English

参考已加入MySQL数据(如JSON表)

[英]Reference JOINed MySQL data like JSON table

The title may be weird, as I'm not sure how to word what I'm doing. 标题可能很奇怪,因为我不确定该如何措辞。

I am building a roster system that will be sorted by designations (think Division, Company, Platoon, etc.). 我正在建立一个花名册系统,将按名称(例如,师,公司,排等)进行排序。 I want this display to be completely dynamic, and I am trying to be as slick as possible in doing it. 我希望这种显示是完全动态的,并且我在尝试时要尽可能地光滑。

In the same database I have 2 tables that are not equal, though they share a couple column names where sometimes the data values are equal, sometimes they aren't. 在同一个数据库中,我有2个不相等的表,尽管它们共享几个列名,有时数据值相等,有时不相等。 The table tali_personnel_designations_weights stores the categories of designation (Division, Company, etc.) while table tali_personnel_designations stores the actual designations (3rd Infantry Division, Kilo Company, etc.). tali_personnel_designations_weights存储名称的类别(师,公司等),而表tali_personnel_designations存储实际的名称(第三步兵师,基洛公司等)。 There are a bunch of things going on, but the answer to 1 will get me back on the road. 发生了很多事情,但是对1的回答会让我回到路上。 Both tables have a column named 'name' which contains, well, the name of that entry, such as Division for the _weights and 3rd Infantry Division for the designation. 这两个表都有一个名为“名称”的列,其中包含该条目的名称,例如_weights的司和第3步兵司的名称。 In my SQL I want to be able to select both names, but as it is below the only return is from tali_personnel_designations_weights, as if the first table was re-written in the result. 在我的SQL中,我希望能够同时选择两个名称,但是由于它下面是唯一的返回值,所以它是从tali_personnel_designations_weights中获得的,就像在结果中重写了第一个表一样。

$SQL = "SELECT * FROM tali_personnel_designations JOIN tali_personnel_designations_weights ON tali_personnel_designations.designation_weight_id=tali_personnel_designations_weights.designation_weight_id ORDER BY tali_personnel_designations_weights.weight DESC, tali_personnel_designations.weight DESC";
$result = mysqli_query($db_handle, $SQL);

while ($db_field = mysqli_fetch_assoc($result)) {
    $name = $db_field['name'];
    echo $name; //output is value for 'name' in tali_personnel_designations_weights
    echo "<br/>";
}

What I'm trying to do, if you can visualize it: 如果您可以可视化,我正在尝试做的是:

$designation_name = $db_field['tali_personnel_designations.name']; //output 3rd Infantry Division
$designationweight_name = $db_field['tali_personnel_designations_weights.name']; //output Division

I understand the easy fix for this is renaming the columns so that 'name' does not conflict, but I'm trying to learn something new here! 我知道解决此问题的简单方法是重命名列,以使“名称”不会冲突,但是我正在尝试在此处学习新知识!

Thanks for the help. 谢谢您的帮助。

Your query should be something like this: 您的查询应该是这样的:

SELECT
    T.*, 
    T.name AS designation_name,
    TW.name AS designation_weight_name
FROM tali_personnel_designations T
JOIN tali_personnel_designations_weights TW
    ON T.designation_weight_id=TW.designation_weight_id
ORDER BY TW.weight DESC, T.weight DESC

Then when using the result you can get the $db_field['designation_name'] and $db_field['designation_weight_name'] , and don't mind of the (also present) $db_field['name'] . 然后,在使用结果时,您可以获得$db_field['designation_name']$db_field['designation_weight_name'] ,不必介意(也存在) $db_field['name']

Note that using alias for the table names makes it much more readable. 请注意,为表名使用别名使其更具可读性。

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

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