1

选择大约三个表,每个表都有不同的字段。一个可能有字段“标题”,另一个可能没有但有字段“位置”。现在我想要这三个表的共同输出,并且每一行都应该有一个标题。对于在输出中具有字段“title”的表,标题应来自字段“title”,但如果表没有“title”但具有“location”,则输出中的标题应来自字段“location”。在常规的 while 循环中,它看起来像这样:

while ($row = mysql_fetch_array($result)) {
   $marker['###TITEL###'] = $row['title']; 
}

我现在想要的是有一个“灵活的”$row['flexible']。所以如果字段“title”存在它应该是$row['title'] 如果它不存在,但是“location,它应该是$row['location']。

有谁知道如何解决这个问题?

问候, 马舍克

4

2 回答 2

2

Use UNION to join the results from all tables, alias the field as flexible, then the result set will contain the flexible column.

SELECT title AS flexible, field2, field3 FROM a
UNION
SELECT location AS flexible, field2, field3 FROM b
于 2010-02-06T11:48:02.507 回答
0

That's how I would do it:

while (list($title, $location, $other) = mysql_fetch_array($result)) {
    if (!empty($title)) {
       $row['title'] = $title;
    } else if (!empty($location)) {
       $row['title'] = $location;
    } else {
       $row['title'] = $other;
    }
}

Sorry if the syntax is not 100% correct. There's a long time that I don't code in php.

Also, the variable that I called $row would be the one that you called $marker in your question.

于 2010-02-06T11:47:32.483 回答