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

  • Home
  • SEARCH
  • 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 6736677
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:09:30+00:00 2026-05-26T11:09:30+00:00

I have a mysql database with two tables, bookings and users. They are linked

  • 0

I have a mysql database with two tables, bookings and users.
They are linked via a foreign key user_id in the bookings table.

How would I go about showing the number of bookings per user.

I’ve been trying variations on this query:

select first_name, last_name, count(*) 
from bookings 
left join users on bookings.user_id 
group by user_id 
order by count(*);

But it’s locking the database for an incredibly long time.

  • 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-05-26T11:09:31+00:00Added an answer on May 26, 2026 at 11:09 am

    First, your ON clause is not complete. Usually it will be ON X=Y, you have ON X. THis is important because you are telling the database how to link your two tables. I’m guessing this is where the most time is lost.

    Second, the LEFT JOIN as used here is useless (unless you want the bookings that have no users.) Try an INNER JOIN.

    Third, as a general rule, it’s better to do SmallTable INNER JOIN BigTable (not always, your mileage may vary.)

    Fourth, as mentioned, use an alias for the ORDER BY clause.

    Try:

    select first_name, last_name, count(*) as cnt
    from users
    inner join bookings on bookings.user_id = user.id
    group by user_id
    order by cnt;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables in my MySQL database, users and tweets, as follows: TABLE
I have two tables in my MySQL database (table1 and table 2). I want
I have a MySQL database with two tables (simplified for this question): movies table
I have two tables in database: comments table users table | u_id | text
I have in my MySQL database these two tables: CREATE TABLE IF NOT EXISTS
I have 10 tables in my database(MySQL). two of them is given below tbl_state
I have a MySQL database with a lot of tables, and I have two
I have two tables in a MySQL database, Locations and Tags, and a third
We have a MySQL database that has two tables, Warehouse and Crate Warehouse: WarehouseID
I have a database that has two tables, one of which contains a foreign

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.