简体   繁体   中英

SELECT statement with an case statement

I have the following table for Customer:

+------------+-------------+------+-----+---------+----------------+
| Field      | Type        | Null | Key | Default | Extra          |
+------------+-------------+------+-----+---------+----------------+
| id         | int(11)     | NO   | PRI | NULL    | auto_increment |
| first      | varchar(45) | YES  |     | NULL    |                |
| last       | varchar(45) | YES  |     | NULL    |                |
| password   | varchar(45) | NO   |     | NULL    |                |
| contact_id | int(11)     | NO   | PRI | NULL    |                |
| address_id | int(11)     | NO   | PRI | NULL    |                |
+------------+-------------+------+-----+---------+----------------+

And the following structure for Appointment:

+-------------+------------+------+-----+---------+----------------+
| Field       | Type       | Null | Key | Default | Extra          |
+-------------+------------+------+-----+---------+----------------+
| id          | int(11)    | NO   | PRI | NULL    | auto_increment |
| time        | datetime   | NO   |     | NULL    |                |
| cancelled   | tinyint(1) | YES  |     | 0       |                |
| confirmed   | tinyint(1) | YES  |     | 0       |                |
| customer_id | int(11)    | NO   | PRI | NULL    |                |
+-------------+------------+------+-----+---------+----------------+

I want to use a single query to get the customer information, if they have appointment information, then it'll query it, otherwise it won't.

I am trying to use the following:

CASE
    WHEN (SELECT count(a.id) FROM appointment 
          INNER JOIN customer c ON a.customer_id = c.id)
        THEN (SELECT c.first, c.last, c.id, a.id FROM appointent
              INNER JOIN customer c ON a.customer_id = c.id)
    ELSE
        (SELECT c.first, c.last, c.id FROM customer)
END;

Do you have any suggestions?

怎么样

SELECT * FROM Customer c LEFT JOIN Appointment a ON a.CustomerId = c.Id

You could make two queries and UNION them.

SELECT c.first, c.last, c.id, a.id FROM appointent a
              INNER JOIN customer c ON a.customer_id = c.id
UNION
SELECT c.first, c.last, c.id, null FROM customer c

Or an outer join, where the a.id would be populated with null if there was no match during the join.

SELECT c.first, c.last, c.id, a.id FROM customer c
              OUTER JOIN appointent a ON a.customer_id = c.id

As per my comment on Zdravko's answer, you could have also used:

select * from customer where id in (select customer_id from appointment where cancelled = 0) 

Which would allow you to filter in a nice way.

You can also filter Zdravko's answer like this:

SELECT * FROM Customer c LEFT JOIN Appointment a ON a.CustomerId = c.Id
WHERE a.cancelled 0 and a.confirmed = 1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM