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 6680053
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:25:48+00:00 2026-05-26T04:25:48+00:00

How do you write a query that executes for each different result in a

  • 0

How do you write a query that executes for each different result in a query? To explain, I run one query to find all the “punch_periods” within a date range:

$search_date = mysql_query("SELECT punch_period 
                            FROM timecards 
                            WHERE emp_id ='1' && 
                            punch_time BETWEEN '$start' AND '$end' 
                            ORDER BY punch_time"); 

while($periods = mysql_fetch_array($search_date)) {

    // run another query for each different punch_period

    }

The result might be 1,1,1,1,2,2. Instead of running a second query 6 times (for each result), I need to run a second query 2 times (for each DIFFERENT punch_period). It will be something like:

$punches = mysql_query("SELECT * FROM timecards 
                        WHERE emp_id ='1' && 
                        punch_period = [RESULT FROM 1ST QUERY]"); 

The reason I’m doing it this way is because I can’t search by date alone- someone may clock in at 11:30pm one day and clock out at 1:00am the following “day”. By searching by unique punch periods, I can get total hours worked in a “workday” even if it overlaps a true calendar “day”.

  • 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-26T04:25:49+00:00Added an answer on May 26, 2026 at 4:25 am

    I fail to see the purpose of the while loop.
    You are just pinging the database server.
    It be much faster to get all rows in one query using a subselect

    SELECT DISTINCT tc.* FROM timecards tc
    WHERE tc.emp_id = '1' 
      AND tc.punch_period IN (SELECT DISTINCT tc2.punch_period 
                           FROM timecards tc2
                           WHERE tc2.emp_id ='1' 
                             AND tc2.punch_time BETWEEN '$start' AND '$end')
    ORDER BY punch_time
    

    Obviously you can simplify this query to:

    SELECT DISTINCT tc.* FROM timecards tc
    WHERE tc.emp_id = '1'
      AND tc.punch_time BETWEEN '$start' AND '$end'
    

    SELECT * is not recommended
    Note that SELECT * is considered an anti-pattern and it is better to explicitly name the fields that you want to select.

    SELECT tc.id, tc.punch_time, tc.punch_period ....
    

    Mixing && and AND
    Don’t mix && and AND, just use AND the mix is confusing and && is not standard SQL IIRC.

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

Sidebar

Related Questions

I need to write a query that returns all object less that or equal
In SQL one can write a query that searches for a name of a
I am trying to write a MySQL query that retrieves one record from table
In SQL you have the ability to write a query that executes a between
How do I write a query that outputs the row number as a column?
I need to write a query that will group a large number of records
I'm trying to write a query that will pull back the two most recent
I'm trying to write a query that extracts and transforms data from a table
I'm trying to write a query that updates rows in a table if a
I am trying to write a query that connects 3 tables. The first table

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.