Igor Mini Minarič Igor Mini Minarič - 1 year ago 99
MySQL Question

Mysql multiple selector

I can not find out how to join this multiple selectors into one. Any help? I already try clausule UNIT but it does not work ether. It is for filtering purpose in html (+php) form using <

input type="text" name="town_from">
as a enter for text value. They work well separately but not both at once.

$sql_1 = "SELECT * FROM ship_db WHERE town_from = '$town_from' AND town_to = '$town_to' ORDER BY top DESC, date_time_of_insert DESC";

$sql_2 = "SELECT * FROM ship_db WHERE price BETWEEN '$price_from' AND '$price_to' ORDER BY top DESC, date_time_of_insert DESC";

Answer Source

Is this what you want?

FROM ship_db s
WHERE (s.town_from = '$town_from' AND s.town_to = '$town_to') OR
      (s.price BETWEEN '$price_from' AND '$price_to')
ORDER BY s.top DESC, s.date_time_of_insert DESC;


  • I am assuming that you want rows that match either criteria (hence OR). If you want both, then use AND.
  • You should be using parameterized queries. That is, don't put user input directly into the query.
  • Presumably, price is a number. You should compare numbers to numbers and dispense with the single quotes (of course, this is also fixed by using parameters).
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download