Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9123507
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T06:21:26+00:00 2026-06-17T06:21:26+00:00

I am having trouble with a select statement. What I have so far is

  • 0

I am having trouble with a select statement. What I have so far is this –

SELECT COUNT(booked.desk_id), 
  name, 
  desk.desk_id, 
  phone, 
  fax, 
  dock, 
  pc 
FROM desk, booked 
WHERE desk.desk_id = booked.desk_id 
  AND booking_id >=1 
  AND location = "Cheltenham"

Which outputs

"12" "Desk 1" "1" "1" "0" "0" "1"

Which is close to what I want, but there is another desk in the desk table called Desk 2, which is it completely ignoring. And indeed, if there are bookings for Desk 2 it includes their count in what it is showing as a count for Desk 1…

Entire table strucutres is as follows:

table "booked"
INSERT INTO `booked` (`id`, `booking_id`, `desk_id`, `member_id`, `date_booked`) VALUES
(246, 1358121601, 1, 1, 'Monday 14th January at 4:40pm'),
(247, 1358121602, 1, 1, 'Monday 14th January at 4:40pm'),
(248, 1358121604, 1, 1, 'Monday 14th January at 4:40pm'),
(249, 1358121603, 1, 1, 'Monday 14th January at 4:40pm'),
(250, 1358121606, 1, 1, 'Monday 14th January at 4:40pm'),
(251, 1358121605, 1, 1, 'Monday 14th January at 4:40pm'),
(252, 1358121607, 2, 1, 'Monday 14th January at 4:40pm'),
(253, 1358121609, 2, 1, 'Monday 14th January at 4:40pm'),
(254, 1358121608, 2, 1, 'Monday 14th January at 4:40pm'),
(255, 1358121610, 2, 1, 'Monday 14th January at 4:40pm'),
(256, 1358121612, 2, 1, 'Monday 14th January at 4:40pm'),
(257, 1358121611, 2, 1, 'Monday 14th January at 4:40pm');

table "desk"
INSERT INTO `desk` (`location`, `desk_id`, `name`, `phone`, `fax`, `dock`, `pc`) VALUES
('Cheltenham', 1, 'Desk 1', 1, 0, 0, 1),
('Cheltenham', 2, 'Desk 2', 1, 1, 0, 1);

What I need help with is how to correctly structure the statement so it will output individual rows for each desk with it’s relevant information.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T06:21:27+00:00Added an answer on June 17, 2026 at 6:21 am

    You are missing a GROUP BY to go along with your aggregate function:

    SELECT COUNT(booked.desk_id), 
      name, 
      desk.desk_id, 
      phone, 
      fax, 
      dock, 
      pc 
    FROM desk
    INNER JOIN booked 
        ON desk.desk_id = booked.desk_id 
    WHERE booking_id >=1 
      AND location = "Cheltenham"
    GROUP BY name;
    

    In MySQL you do not have to GROUP BY all fields in the select list, but in other RDBMS you would have to use:

    SELECT COUNT(booked.desk_id), 
      name, 
      desk.desk_id, 
      phone, 
      fax, 
      dock, 
      pc 
    FROM desk
    INNER JOIN booked 
        ON desk.desk_id = booked.desk_id 
    WHERE booking_id >=1 
      AND location = "Cheltenham"
    GROUP BY name, desk.desk_id, phone, fax, dock, pc 
    

    Based on your sample data and comment, you can use:

    SELECT coalesce(CountDesk, 0) Total, 
      name, 
      d.desk_id, 
      phone, 
      fax, 
      dock, 
      pc 
    FROM desk d
    LEFT JOIN
    (
      select COUNT(booked.desk_id) CountDesk,
        desk_id
      from booked 
      WHERE booking_id >=1 
      GROUP BY desk_id
    ) b
        ON d.desk_id = b.desk_id 
    WHERE location = "Cheltenham"
    

    See SQL Fiddle with Demo

    If you want to do this without the subquery:

    SELECT 
      Coalesce(count(b.desk_id), 0) Total, 
      name, 
      d.desk_id, 
      phone, 
      fax, 
      dock, 
      pc 
    FROM desk d
    LEFT JOIN booked b
      ON d.desk_id = b.desk_id 
    WHERE booking_id >=1 
      AND location = "Cheltenham"
    GROUP BY name, d.desk_id, phone, fax, dock, pc ;
    

    See SQL Fiddle with Demo

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having trouble with Microsoft Access 2003, it's complaining about this statement: select cardnr
I'm having trouble wrapping my brain around this select statement. The data is coming
I'm having trouble figuring out how to represent the following JPQL query: SELECT count(e)
I have a statement like this: SELECT CONCAT(DATE(`starttime`),' ',HOUR(`starttime`),':',LPAD(1*(MINUTE(`starttime`) DIV 1),2,'0'),':00') AS date FROM
SQL Server beginner here. I am having some trouble on a SELECT/UPDATE statement. I
I'm having trouble using a select statement to set a return value in a
I have a SQL query I'm having trouble creating using NHibernate Criteria: SELECT ID,
I'm having trouble converting the following SQL-statement to LINQ-to-entities: SELECT l.* FROM locations l
I'm having trouble with my SQL SELECT statement. Basically I want to return all
I am having trouble writing the result of my SELECT statement into an excel

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.