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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T19:06:06+00:00 2026-05-24T19:06:06+00:00

I have the following table structure. One table EQUITIES and a table for each

  • 0

I have the following table structure. One table EQUITIES and a table for each row in this table.

EQUITIES table:
id instrument

and tables for every instrument with are all similar to this:
Tables named like EE5367126893 (various names and the names are stored in equities table).

EE5367126893
id chg 

EDIT:
My code is BAD. It somehow gets me wrong results and i can’t seem to find an error.

<?
//CREATION OF TOP MOVERS
include('connect.php');
$sql = "SELECT * FROM equities";     

$result = mysql_query($sql) or die(mysql_error());

$instruments = array();
while ($row = mysql_fetch_array($result)) {
    array_push($instruments, $row["instrument"]);
}
$i=0;
foreach($instruments as $instrument) {
    $sqlx .= "(SELECT id, chg, vol, '$instrument' as name FROM ".$instrument." ORDER BY id DESC LIMIT 1)";
    if ($i<count($instruments)-1){
            $sqlx .= " UNION ";
            $i++;
    }
}
$gsql = $sqlx;
$lsql = $sqlx;
$vsql = $sqlx;

//Gainers
$gsql .= " ORDER BY chg DESC LIMIT 3";
$gresult = mysql_query($gsql) or die(mysql_error());
while ($grow = mysql_fetch_array($gresult)) {
    $g[$grow['name']] = $grow['chg'];;
}
print_r($g);
//losers
$lsql .= " ORDER BY chg ASC LIMIT 3";
$lresult = mysql_query($lsql) or die(mysql_error());
while ($lrow = mysql_fetch_array($lresult)) {
    $l[$lrow['name']] = $lrow['chg'];;
}
print_r($l);
//most volume
$vsql .= " ORDER BY vol DESC LIMIT 3";
$vresult = mysql_query($vsql) or die(mysql_error());
while ($vrow = mysql_fetch_array($vresult)) {
    $v[$vrow['name']] = $vrow['vol'];;
}
print_r($v);
?>

It got me results:

Array
(
    [LV0057869] => 0.68
    [EE310054309] => 0.00
    [EE3100034553] => -5.03
)
Array
(
    [LV0054359] => -0.84
    [LT0000543337] => -3.83
    [LT00453127375] => -4.03
)
Array
(
    [EE310054334653] => 791
    [EE3100003609] => 58538
    [LT000543337] => 33240
)

Its pretty obvious that I’m not getting the the highest, lowest or values with most vol (as -0.84>-5.03).It looks like random values got locked with this query. Where’s the catch? Im pretty sure theres something bad with my sql query…

  • 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-24T19:06:06+00:00Added an answer on May 24, 2026 at 7:06 pm

    You can add name to select

    SELECT id, chg, 'table_name or instrument_name' as inst_name from ...
    

    Example:

    $sqlx .= "(SELECT id, chg, '$instrument' as name FROM ".$instrument." ORDER BY id DESC LIMIT 1)";
    

    It seems that vol has got VARCHAR type and rows are sorted by chars not by integers.
    Could you provide your table scheme?

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

Sidebar

Related Questions

I have already googled for this I have a Table with following structure in
I have the following table structure, A -< B represents one A can have
I have a table structure similar to the following example: DateTime V1 V2 V3
I have a table structure looking something like this. One outer table and in
I have the following database structure: Table 1 Table 2 Table 3 tid_1 ----(many-to-one)----
I have following table structure: Table: Plant PlantID: Primary Key PlantName: String Table: Party
I have the following table structure CREATE TABLE `table` ( `id` int(11) NOT NULL
I have the following table structure (in MySQL): DocID, Code, IsDup, DopOf , where
I have the following table structure. A | B | C I wish to
I have the following table structure, which is imported into an Entity Framework project:

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.