0

I have two sql tables :

table_A : 
ID     VALUE
1      a
2      b
3      c
4      d

table_B : 
ID     VALUE
2      oA
4      oB
1      oC
3      oD

I'm looking for an sql query for get this php array :

$myarray = (
    "a" => "oC",
    "b" => "oA",
    "c" => "oD",
    "d" => "oB",
);

Is it possible to get this result with one query ? Can you help me ?

1
  • 2
    Yes it's possible. Commented Dec 12, 2016 at 15:45

2 Answers 2

1

You can do that with an INNER JOIN. Here's a PDO example:

$sql = 'SELECT a.VALUE AS aVal, b.VALUE AS bVal FROM table_A a INNER JOIN table_B b ON a.id = b.id';
foreach ($pdo->query($sql) as $row) {
    $result[$row['aVal']] = $row['bVal'];
}
Sign up to request clarification or add additional context in comments.

Comments

1

or...

Select '"' + a.Value + '" => "' + b.Value + '",'
From table_A a join table_B b
   on b.Id = a.Id

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.