Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

SQL search many conditions must return one row

Problem is that i get a whole bunch of users, that does not match the search string no matter what i search. And it seems like there is baseline of users that is allways passed. I cant figure out what exactly it is finding, but the lenght of the return of users vary depending on what i search.

I need to return all the rows

WHERE orders.restaurant_fk = :restaurant_id (must allwayss be true) OR users2.user_name LIKE :search 

OR users2.user_last_name LIKE :search OR users2.user_address LIKE :search

By that i mean: if i search for a string value i want to get an order that has a single match in the search query or multiple.
The important is that i cannot get same order twice.
Forexample if the same user has a email called "jens@jens.dk" and a user_name called "jens". it should only be returned once

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Another important thing is that the orders must contain the specific restaurant_id.

<?php

require_once __DIR__ . '/../_.php';

session_start();
try {
    $json = file_get_contents('php://input');
    $data = json_decode($json);

    $db = _db();
    $restaurant_id = $data->restaurant_id;
    $search = $data->search;

    $q = $db->prepare(
        'SELECT DISTINCT restaurants.restaurant_id, orders.*, users2.user_name, users2.user_last_name, users2.user_address, users2.user_city, users2.user_zip
        FROM orders
        JOIN users2 ON orders.user_fk = users2.user_id
        JOIN restaurants ON orders.restaurant_fk = restaurants.restaurant_id
        WHERE orders.restaurant_fk = :restaurant_id
        OR users2.user_name LIKE :search
        OR users2.user_last_name LIKE :search
        OR users2.user_address LIKE :search
        ORDER BY orders.created_at DESC'
    );
    $q->bindValue(':restaurant_id', $restaurant_id);
    $q->bindValue(':search', "%{$search}%");
    $q->execute();
    $orders = $q->fetchAll();


    echo json_encode(['orders' => $orders]);
} catch (Exception $e) {
    try {
        if (!$e->getCode() || !$e->getMessage()) {
            throw new Exception();
        }
        var_dump($e);
        http_response_code($e->getCode());
        echo json_encode(['info' => $e->getMessage()]);
    } catch (Exception $ex) {
        http_response_code(500);
        echo json_encode($ex);
    }
}

Tables:
I am using case insensitive MYSQL database.

Orders Table

enter image description here
Users2 Table

enter image description here

>Solution :

Sounds like you need some AND OR conditions

SELECT
  restaurants.restaurant_id,
  orders.*,
  users2.user_name,
  users2.user_last_name,
  users2.user_address,
  users2.user_city,
  users2.user_zip
FROM orders
JOIN users2 ON orders.user_fk = users2.user_id
JOIN restaurants ON orders.restaurant_fk = restaurants.restaurant_id
WHERE orders.restaurant_fk = :restaurant_id
  AND (
    users2.user_name LIKE :search
    OR users2.user_last_name LIKE :search
    OR users2.user_address LIKE :search
  )
ORDER BY orders.created_at DESC;

You can alternatively move some conditions to the joins

SELECT
  restaurants.restaurant_id,
  orders.*,
  users2.user_name,
  users2.user_last_name,
  users2.user_address,
  users2.user_city,
  users2.user_zip
FROM orders
JOIN users2 ON orders.user_fk = users2.user_id
JOIN restaurants
  ON orders.restaurant_fk = restaurants.restaurant_id
 AND orders.restaurant_fk = :restaurant_id
WHERE (
    users2.user_name LIKE :search
    OR users2.user_last_name LIKE :search
    OR users2.user_address LIKE :search
  )
ORDER BY orders.created_at DESC;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading