Q
Q
qwas19902018-07-05 16:08:31
PHP
qwas1990, 2018-07-05 16:08:31

Combining two sql queries?

There are two queries to the database (almost identical):
1. makes a selection from tables with the condition that one value is greater than 0 and then sorts by one column
2. makes a selection from the same tables with the condition that one value = 0 and then sorts by another column
1.SELECT * FROM ..... WHERE quantity>0 ORDER BY quantity
2.SELECT * FROM ..... WHERE quantity=0 ORDER BY name The
question is how to combine two queries using sql or results with php

Answer the question

In order to leave comments, you need to log in

4 answer(s)
D
Dmitry Dart, 2018-07-05
@gobananas

UNION
www.sql.ru/docs/sql/u_sql/ch14.shtml

Q
qwas1990, 2018-07-05
@qwas1990

UNION already tried
if done in the form
(SELECT * FROM ..... WHERE quantity>0 ORDER BY quantity) UNION (SELECT * FROM ..... WHERE quantity=0 ORDER BY name) sorting is not saved

B
BI0R0B0T, 2018-07-05
@BI0R0B0T

You can add an additional column and take it into account in sorting

SELECT tblName.* , if(quantity=0, 1,0) as x FROM tblName... ORDER BY x, quantity, name

I
Ivan Lykov, 2018-07-10
@Jhon_Light

leftJoin to help you

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question