views:

220

answers:

2

Hi i got a mysql-select about three tables, where each tables have different fields. One may have the field "title", the other may not but have the field "location". Now i want to have a common output of these three tables, and each row is supposed to have a title. for tables, whoch have the field "title" in the output the title should come from field "title" but if the table hasnt "title" but has "location" the title in the output should come from field "location". In a regular while-loop it would look like this:

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

what i want now is to have like a "flexible" $row['flexible']. So if field "title" exists it should be $row['title'] if it doesnt, but "location, it should be $row['location'].

Does anyone have an idea how to solve this?

Regards, Maschek

A: 

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.

Bruno Rothgiesser
+2  A: 

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
Leventix
+1, but I don't think it's necessary to alias the columns in the second part of the query, the fields names are set in the first part of the union. You just have to select the same number of fields in the second part. No harm done tho and it is probably more understandable when you read it.
meouw
I am currently trying to use your example @leventix, (i want to place the aliases, when the structur is working) have a look on the code, unfortunately, no result is displayed:$query = "SELECT * FROM messages a LEFT JOIN uid_connector c ON c.connect_uid = a.uid UNION SELECT * FROM news b LEFT JOIN uid_connector c ON c.connect_uid = b.uid"; any Ideas?
maschek
i could solve it now by using your example, did some modifications, for example added brackets around the selects. now it works. thank you...
maschek