0

I have two tables, company and openhours.

company id | name

openhours id | day | company | open | close

openhours has 7 rows per company, with each days open hours ('mon', 'tue', 'wed' etc)

Basically I want to fetch the openhours for both wednesday (wed) and thursday (thu)

SELECT * FROM openhours WHERE company=1 AND day='wed'
SELECT * FROM openhours WHERE company=1 AND day='thu'

What I'm looking for though is getting both the company and openhours for the company in the same query, but so far I can only get 1 row for openhours

SELECT company.*, openhours.* FROM company INNER JOIN openhours ON openhours.company=company.id WHERE openhours.day == 'wed' OR openhours.day == 'thu

Any help? Been sitting on this for quite some time now

EDIT This is how the openhours table looks like (company id 1)

id | day | company | open | close

1 mon 1 08:00 16:00
2 tue 1 08:00 16:00
3 wed 1 10:00 16:00
4 thu 1 11:00 15:00
5 fri 1 08:00 16:00
6 sat 1 11:00 14:00
7 sun 1 11:00 14:00
3
  • You mentioned the query only gives you one record, what are you using to look at the results of your query? Commented Jan 9, 2014 at 21:20
  • Please post the data. Commented Jan 9, 2014 at 21:23
  • See edit on first post Commented Jan 9, 2014 at 21:26

4 Answers 4

2
SELECT
  company.*, openhours.*
FROM
  company
  INNER JOIN
    openhours ON openhours.company=company.id
WHERE
  company.id = 1 AND
  openhours.day IN ('wed', 'thu')
;

You say you still get 1 row, so I think there is some other issue because it worked fine for me in this SQLFiddle. How are you calling this query? There may be a problem in your execution or environment. You could also try a LEFT JOIN, but this should be INNER.

Sign up to request clarification or add additional context in comments.

5 Comments

Only gives me 1 record for openhours which is for wednesday. I'm looking to get both for wednesday and thursday. Thank you for your input though, really appreciate it!
Same issue, only gives me the record for wednesday (wed)
Weird. Can you try changing company.id to openhours.company?
Same problem. But isn't it bound to only return 1 record from the openhours table, whichever is first (which is wednesday in this case).
@Emil please view my SQLFiddle, everything should be working fine.
1
SELECT company.*, openhours.* 
FROM company INNER JOIN openhours ON openhours.company=company.id 
WHERE (day='wed' OR day='thu')

5 Comments

Only gives me 1 record for openhours which is for wednesday.
Can you please post the data of your database query for selecting all opening hours for your company. Are you sure that there are opnening hours for Thursday in your DB?
Yes I'm sure there are opening hours for thursday. Your query is bound to only return one record from the openhours table, whichever is first (which is wednesday in this case)
I dont think so as there is no LIMIT at the and of the query.
I've tried every single modification of this as it is but always return the first match (wednesday). Try for yourself if you like. Been stuck on this for 2 days now
0

What about:

SELECT * FROM openhours WHERE company=1 AND (day='wed' OR day='thu');

or:

SELECT company.*, openhours.* FROM company INNER JOIN openhours ON openhours.company = company.id WHERE openhours.day IN ('wed', 'thu')

Your query had a '==' (syntax error).

1 Comment

Same as the answers above. Feels like I've tried everything. What I need is to get both the record for wednesday (wed) and thursday (thu)
0
Select *
from company c, openhours o
where c.id=o.company and c.id=1 and o.day='wed' or o.day='thu';

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.