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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T01:26:31+00:00 2026-06-03T01:26:31+00:00

$STH_1 = $DBH_R->query(SELECT table_name FROM information_schema.tables WHERE table_name LIKE ‘v_c_ei\_9%’ ); $stmts_1 = array();

  • 0
$STH_1 = $DBH_R->query("SELECT table_name                        
                         FROM information_schema.tables                        
                        WHERE table_name                        
                         LIKE 'v_c_ei\_9%'
                         ");
$stmts_1 = array();

while (($row_1 = $STH_1 -> fetch_assoc()) !== null){

$table_name = $row_1['table_name'];
$stmts_1[] = sprintf("SELECT *  
                        FROM $table_name
                       WHERE (ei_code = '1117') AND (DAY(date_time) != '00') 

                     "); 

} 

$stmt_1 = implode("\nUNION\n", $stmts_1);  
$stmt_1 .= "\nORDER BY date_time ASC";  

$STH_5_2 = $DBH_R->query($stmt_1);

while (($row_5_2 = $STH_5_2 -> fetch_assoc()) !== null){

OK, the script above is working ok. But, I want (must) change its functionality. I must do selection before UNION (something like ORDER BY … LIMIT) but I know – this is not working. The solution is to change sprintif into UNION & UNION & UNION … But the problem is – we don’t know how much tables we have.

The table names from the first query looks like

v_c_ei_9001
v_c_ei_9002
..........
v_c_ei_9030
..........
ect.

Than the q: how change the sprintif (and maybe array $stmts_1 too) from abowe into the UNION in situation when we don’t know how much tables we have?
thnks

  • 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-03T01:26:33+00:00Added an answer on June 3, 2026 at 1:26 am

    When you add ORDER BY and LIMIT clauses to each constituent of the UNION, as follows:

    while (($row_1 = $STH_1 -> fetch_assoc()) !== null){
    
    $table_name = $row_1['table_name'];
    $stmts_1[] = sprintf("SELECT *  
                          FROM $table_name
                          WHERE (ei_code = '1117') AND (DAY(date_time) != '00')
                          ORDER BY date_time DESC LIMIT 1 
                         "); 
    
    } 
    

    And then you do the following:

    $stmt_1 = implode("\nUNION\n", $stmts_1);  
    $stmt_1 .= "\nORDER BY date_time ASC";                   // <---- remember this bit
    

    The resulting string in $stmt_1 will look something like this:

    SELECT *
    FROM v_c_ei_9001
    WHERE (ei_code = '1117') AND (DAY(date_time) != '00')
    ORDER BY date_time DESC LIMIT 1
    UNION
    ...
    UNION
    SELECT *
    FROM v_c_ei_9030
    WHERE (ei_code = '1117') AND (DAY(date_time) != '00')
    ORDER BY date_time DESC LIMIT 1
    ORDER BY date_time ASC                                 -- it's appearing down here!
    

    As you can see, the final constituent of your UNION has two ORDER BY clauses, which is a syntax error. Either remove the line commented remember this bit above, or else (if you need to reorder the unified queries) you must make the UNION a subquery to an outer query that performs the reordering:

    $stmt_1 = "SELECT * FROM ("
    $stmt_1 = implode("\nUNION\n", $stmts_1);  
    $stmt_1 .= ") ORDER BY date_time ASC";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm copying ListViewItems from one ListView to another, sth. like: foreach (ListViewItem item in
i have these: a=sc.array([0,1]) b=sc.array([1,0]) c=sc.array([0,-1]) d=sc.array([-1,0]) orientation_list=(sc.array([a,b,c,d])).tolist() #--------------------------------------------------------------------- #i am trying sth like
I would like to extract some lines from a text file, I have started
I'm implementing global search box extension (sth like SearchableDictionary sample in android sdk). Everything
means, do sth, then request using jQuery, like a hooker. How can i do
I would like to use substitution feature of donut caching. public static string GetTime(HttpContext
I would like to do something like: for(int i=0;i<10;i++) addresses[i] = & function(){ callSomeFunction(i)
I'm looking for a regex that will change sth. like this: print testcode $testvar
I have a wxPython application with some code like below. I want to set
I have a collection with entries that look like : { userid: 1, contents:

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.