Home > Back-end >  SQL How to make an OR Function only display one result
SQL How to make an OR Function only display one result

Time:10-14

For example, I have two columns, Date and Hours and from a variety of dates, I only want it to display one result.

SELECT Day, Hours
FROM Hours
WHERE (Day)=#2021-04-22# OR (Day)=#2021-04-23#;

Displays

Day         Hours
2021-04-22  9
2021-04-22  2
2021-04-23  6
2021-04-23  3

But I would like for it to only choose to display one of the two dates.

CodePudding user response:

You simply want to add "LIMIT 1" to the end of the query. Like this:

SELECT
  Day,
  Hours
FROM
  Hours
WHERE
  Day = '2021-04-22'
  OR Day = '2021-04-23'
LIMIT
  1;

(I changed the syntax a little bit. I personally think it's more clean.)

CodePudding user response:

Select TOP N is used to specify to display only N number of records. In this example, it will return only one record and select the day and hours (2 rows)

SELECT Day, Hours
FROM Hours
WHERE Day = (
            SELECT TOP 1 Day FROM Hours
            WHERE (Day)=#2021-04-22# OR (Day)=#2021-04-23#
            )
  • Related