1
1
1010101010102014-05-17 11:52:02
MySQL
101010101010, 2014-05-17 11:52:02

Selecting and joining data from 2 mysql tables

hello, I'm trying to solve the following problem:
there is a table with id , m1 , m2
fields m1 and m2 of the same type, which cannot be null at the same time.
you need to make a selection, from n records sorted by id
records are selected in this way
if m1 is null , then the record is not taken.
if m1 is not null , then 2 cases are possible
1) if m2 is not null , then m2 AS m1 is taken
2) if m2 is null , then m1 is taken
I can do it separately, i.e. select n records of the form 1) and n records of the form 2 ) and create the resulting array using some language, but I would like everything in sql, if possible.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
Y
Yago, 2014-05-17
@101010101010

Use an IF statement:
www.mysql.ru/docs/man/Control_flow_functions.html
It can also be nested:
komlenic.com/254/mysql-nested-if-in-select-queries

S
Sergey Pankov, 2014-05-20
@trapwalker

IF's will only confuse everything. There is a special coalesce function for your task :

select coalesce(m2, m1)
from my_table
where m1 is not null

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question