Query to collect data from multiple columns of the same row, by Primary Key of the record

Asked

Viewed 55 times

0

all quiet?!

See if you can help me!

From the query below (use phpMyAdmin), I only need to bring the results of "LETRAA", "LETRAT", "LETRAG" and "LETRAF" to the browser screen (via PHP), but I do not know which part of the code of the query should be changed to get the result I need.

SELECT
    tr_id,
    LENGTH(TAB.RESPOSTAS)-LENGTH(REPLACE(TAB.RESPOSTAS,'A','')) LETRAA,
    LENGTH(TAB.RESPOSTAS)-LENGTH(REPLACE(TAB.RESPOSTAS,'T','')) LETRAT,
    LENGTH(TAB.RESPOSTAS)-LENGTH(REPLACE(TAB.RESPOSTAS,'G','')) LETRAG,
    LENGTH(TAB.RESPOSTAS)-LENGTH(REPLACE(TAB.RESPOSTAS,'F','')) LETRAF
FROM (
    SELECT
        tr_id,
        concat(
        tr_1 ,tr_2 ,tr_3 ,tr_4 ,tr_5 ,tr_6 ,tr_7 ,tr_8 ,tr_9 ,tr_10,
        tr_11,tr_12,tr_13,tr_14,tr_15,tr_16,tr_17,tr_18,tr_19,tr_20,
        tr_21,tr_22,tr_23,tr_24,tr_25,tr_26,tr_27,tr_28,tr_29,tr_30,
        tr_31,tr_32,tr_33,tr_34,tr_35,tr_36,tr_37,tr_38,tr_39,tr_40,
        tr_41,tr_42,tr_43,tr_44,tr_45,tr_46,tr_47,tr_48,tr_49,tr_50,
        tr_51,tr_52,tr_53,tr_54,tr_55,tr_56,tr_57,tr_58,tr_59,tr_60,
        tr_61,tr_52,tr_63,tr_64,tr_65,tr_66,tr_67,tr_68,tr_69,tr_70,
        tr_71,tr_52,tr_73,tr_74,tr_75,tr_76,tr_77,tr_78,tr_79,tr_80,
        tr_81
        ) RESPOSTAS
    FROM
        tetra_respostas
    WHERE
        tr_id = 11
) TAB

To better understand, below is an image of the expected result:

inserir a descrição da imagem aqui

Below, the code of the page to be displayed, but still the data is not coming from the query. So I think it’s something in the query code.

    <tr>
      <td><?php echo $row_Tetragrama['tr_id']; ?></td>
      <td><?php echo $row_Tetragrama['RESPOSTAS']; ?></td>
      <td><?php echo $row_Tetragrama['LETRAA']; ?></td>
      <td><?php echo $row_Tetragrama['LETRAT']; ?></td>
      <td><?php echo $row_Tetragrama['LETRAG']; ?></td>
      <td><?php echo $row_Tetragrama['LETRAF']; ?></td>
    </tr>
  • As you say that the result in phpMyAdmin is correct then everything suggests that the problem is not with the SQL expression, but maybe I didn’t understand your question.

  • It is... my doubt is that, although the PHP and SQL codes are running "well", the data of the columns "LETRAA", "LETRAT", "LETRAG" and "LETRAF" are not displayed in the browser.

2 answers

0

If the query in Myphpadmin is bringing the desired result, all that is missing is to print the value of the variables in each of your table.

You can see that you managed to print code 11 in the tr_id column; you just need to print the others on each

You must have done something like this:

<tr>
  <td><?php print $resultado['tr_id'];?></td>
  <td></td>
  <td></td>
  <td></td>
  <td></td>
  <td></td>
</tr>

It’s all about completing:

<tr>
  <td><?php print $resultado['tr_id'];?></td>
  <td></td>
  <td><?php print $resultado['LETRAA'];?></td>
  <td><?php print $resultado['LETRAT'];?></td>
  <td><?php print $resultado['LETRAG'];?></td>
  <td><?php print $resultado['LETRAF'];?></td>
</tr>
  • The PHP code I have here is correct. It is the same as your suggestion. The problem is in the query code... something there does not allow me to display the result by letter in the browser.

  • If you use aliases in the column name, does the error persist? Example: SELECT tr_id, LENGTH(TAB.ANSWERS)-LENGTH(REPLACE(TAB.ANSWERS,'A','')) as LETRAA,

  • Yes, persist. What a strange thing!

  • I finally found the mistake!!! They are on lines 17 and 17 of the query, on: "tr_52" and "tr_52", where the correct is: "tr_62" and "tr_72", respectively... so the results were not being displayed as expected.

0


I finally found the mistake!!!

They are on lines 17 and 18 of the query, on: "tr_52" and "tr_52", where the correct is: "tr_62" and "tr_72", respectively... so the results were not being displayed as expected.

SELECT
tr_id,
LENGTH(TAB.RESPOSTAS)-LENGTH(REPLACE(TAB.RESPOSTAS,'A','')) LETRAA,
LENGTH(TAB.RESPOSTAS)-LENGTH(REPLACE(TAB.RESPOSTAS,'T','')) LETRAT,
LENGTH(TAB.RESPOSTAS)-LENGTH(REPLACE(TAB.RESPOSTAS,'G','')) LETRAG,
LENGTH(TAB.RESPOSTAS)-LENGTH(REPLACE(TAB.RESPOSTAS,'F','')) LETRAF
  FROM (
   SELECT
    tr_id,
    concat(
    tr_1, tr_2 ,tr_3 ,tr_4 ,tr_5 ,tr_6 ,tr_7 ,tr_8 ,tr_9 ,tr_10,
    tr_11,tr_12,tr_13,tr_14,tr_15,tr_16,tr_17,tr_18,tr_19,tr_20,
    tr_21,tr_22,tr_23,tr_24,tr_25,tr_26,tr_27,tr_28,tr_29,tr_30,
    tr_31,tr_32,tr_33,tr_34,tr_35,tr_36,tr_37,tr_38,tr_39,tr_40,
    tr_41,tr_42,tr_43,tr_44,tr_45,tr_46,tr_47,tr_48,tr_49,tr_50,
    tr_51,tr_52,tr_53,tr_54,tr_55,tr_56,tr_57,tr_58,tr_59,tr_60,
    tr_61,tr_62,tr_63,tr_64,tr_65,tr_66,tr_67,tr_68,tr_69,tr_70,
    tr_71,tr_72,tr_73,tr_74,tr_75,tr_76,tr_77,tr_78,tr_79,tr_80,
    tr_81
    ) RESPOSTAS
   FROM
    tetra_respostas
   WHERE
    tr_id = idResp
) TAB

I always suspected some error in SQL... Ufa!!! Follow the game...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.