DaHaKa
DaHaKa

Reputation: 161

Retrieving info from mysql tables

I have such a situation. I have two mysql tables. One "data" table and second table "mirror_data". "data" table consists columns "local" (in this column is stored just a numbers). and this is "mirror_data" table

CREATE TABLE IF NOT EXISTS mirror_data
    (
    id_mir INT(11) NOT NULL UNIQUE,
    local VARCHAR(255),
    local_mir VARCHAR(255),
    id_data INT(11) NOT NULL DEFAULT 0
    );

And whats this second table means => I've marked that local column consists just a numbers and in this (second table mirror_data) include in local column same information (just like in "data" table), but also consists local_mir column , which marks persons names , lets says that this is just some strings.

And Here is the situation => when i am retrieving information from "data" table , want to check for each information if exists local_mir string (from "mirror_data" table) for current local (from "data" table) column and if it exists retrieve this string from local_mir instead of numbers (from "data" table from local column).

Is any idea how to do that?

Upvotes: 1

Views: 141

Answers (1)

Mithrandir
Mithrandir

Reputation: 25337

I'm not exactly sure, if i get what you want, but is it something like this?

SELECT
     data.id,
     case
        when mirror_data.local_mir is not null then mirror_data.local_mir
        else
          data.local
     end local
FROM 
data
LEFT JOIN
mirror_data
ON data.id = mirror_data.id_mir

OR use the COALESCE function ....

Upvotes: 2

Related Questions