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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:48:08+00:00 2026-06-15T13:48:08+00:00

I am trying to piece two queries together. Below is the code I’m using.

  • 0

I am trying to piece two queries together. Below is the code I’m using. However the table is splitting up the data. How can I remedy this? Or what better solutions are there?

while($row = mysql_fetch_array($result))
{
    echo "<tr id='centered' >";  
    echo "<td class='leftalign'>" . $row['Quarter_Name'] . "</td>";
    echo "<td>" . $row['Quarterly_yield'] . "</td>";
    echo "<td>" . $row['Quarterly_yield'] . "</td>";
    echo "<td>" . $row['Quarterly_yield'] . "</td>";
}

while($row = mysql_fetch_array($result8))
{
    echo "<td>" . $row['Quarterly_yield'] . "</td>";
} 

The 2 Queries are as follows: They are nearly identical

SELECT  LEFT(A.F_ANOTRIMESTRE, 4) Year,
        RIGHT(A.F_ANOTRIMESTRE, 2) Quarter,
        IF(RIGHT(A.F_ANOTRIMESTRE, 2)=03,'Enero a Marzo',
            IF(RIGHT(A.F_ANOTRIMESTRE, 2)=06,'Abril a Junio',
                IF(RIGHT(A.F_ANOTRIMESTRE, 2)=09,'Julio a Septiembre',
                    IF(RIGHT(A.F_ANOTRIMESTRE, 2)=12,'Octubre a Diciembre', '')
                )
            )
        ) Quarter_Name,
        ROUND(A.POR_RENTABILIDAD, 2) Quarterly_yield
FROM    dr_rent_carteras_trimestres A
WHERE   A.ID_CARTERA = $ID_CARTERA
AND     A.IND_RENTABILIDAD = 1
AND     LEFT(A.F_ANOTRIMESTRE, 4) = (
            SELECT MAX(left(F_ANOTRIMESTRE, 4)) - 0
            FROM   dr_rent_carteras_trimestres
            WHERE  ID_CARTERA = $ID_CARTERA 
        )

Here is the 2nd one:

SELECT  LEFT(A.F_ANOTRIMESTRE, 4) Year,
        RIGHT(A.F_ANOTRIMESTRE, 2) Quarter,
        IF(RIGHT(A.F_ANOTRIMESTRE, 2)=03,'Enero a Marzo',
            IF(RIGHT(A.F_ANOTRIMESTRE, 2)=06,'Abril a Junio',
                IF(RIGHT(A.F_ANOTRIMESTRE, 2)=09,'Julio a Septiembre',
                    IF(RIGHT(A.F_ANOTRIMESTRE, 2)=12,'Octubre a Diciembre', '')
                )
            )
        ) Quarter_Name,
        ROUND(A.POR_RENTABILIDAD, 2) Quarterly_yield
FROM    dr_rent_carteras_trimestres A
WHERE   A.ID_CARTERA = $ID_CARTERA
AND     A.IND_RENTABILIDAD = 1
AND     LEFT(A.F_ANOTRIMESTRE, 4) = ( 
            SELECT MAX(left(F_ANOTRIMESTRE, 4)) - 1
            FROM   dr_rent_carteras_trimestres
            WHERE  ID_CARTERA = $ID_CARTERA 
        )
  • 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-15T13:48:09+00:00Added an answer on June 15, 2026 at 1:48 pm

    In your code above you open the tr tag in the first loop but never close it. Also in the first loop you print out the Quarterly_yield three times yet there is one column calculated for that result. Effectively you are printing out the same thing.

    A couple of options – Print each result from each query in its own row:

    // Loop through the results and print 
    while($row = mysql_fetch_array($result))
    {
        echo "<tr id='centered' >\m";  
        echo "<td class='leftalign'>{$row['Quarter_Name']}</td>\n";
        echo "<td>{$row['Quarterly_yield']}</td>\n";
        echo "</tr>\n";
    }
    
    while($row = mysql_fetch_array($result8))
    {
        echo "<tr id='centered' >\m";  
        echo "<td class='leftalign'>{$row['Quarter_Name']}</td>\n";
        echo "<td>{$row['Quarterly_yield']}</td>\n";
        echo "</tr>\n";
    }
    

    Or you can create an array with the results you have and then print that array. This effectively will join the results together. So let’s assume that the array is structured as such:

    $results[Year-Quarter] = array(Year, Quarter, QuarterName, Result1, Result2)
    

    then you can construct the array as follows:

    $results = array();
    
    while($row = mysql_fetch_array($result))
    {
        $key = $row['Year'] . '-' . $row['Quarter'];
        $results[$key] = array(
            'Year'            => $row['Year'],
            'Quarter'         => $row['Quarter'],
            'Quarter_Name'    => $row['Quarter_Name'],
            'Quarter_yield_1' => $row['Quarter_yield'],
            'Quarter_yield_2' => 0,
        );
    }
    
    while($row = mysql_fetch_array($result8))
    {
        $key = $row['Year'] . '-' . $row['Quarter'];
    
        // Check if we have this key
        if (isset($results[$key]))
        {
            $results[$key]['Quarter_yield_2'] = $row['Quarter_yield'];
        }
        else
        {
            $results[$key] = array(
                'Year'            => $row['Year'],
                'Quarter'         => $row['Quarter'],
                'Quarter_Name'    => $row['Quarter_Name'],
                'Quarter_yield_1' => 0,
                'Quarter_yield_2' => $row['Quarter_yield'],
            );
    }
    

    and then print the results from the $results array

    foreach ($results as $item)
    {
        echo "<tr id='centered' >\m";  
        echo "<td class='leftalign'>{$item['Quarter_Name']}</td>\n";
        echo "<td>{$item['Quarter_yield_1']}</td>\n";
        echo "<td>{$item['Quarter_yield_2']}</td>\n";
        echo "</tr>\n";
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a piece of code that takes in a two digit
Trying to write a piece of code to compare two strings. If either are
I am trying to write a short piece of html code that, given two
I'm trying to piece together info from these two answers, but neither seems to
I've been trying to parallelize this piece of code for about two days and
I'm trying to write out a piece of code that can reduce the LENGTH
I am trying to write a short piece of html code that, given two
I'm trying to write a piece of code that can automatically factor an expression.
i am trying a piece of code. <?php $tmp = ord('F'); //gives the decimal
I am trying the following piece of code in java, but it doesn't seem

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.