Concatenate select result

Asked

Viewed 652 times

3

I’m having a hard time showing the results of select the way I want it. I’ll explain first what I have and then what I want, I have two tables in the database:

Product type: Product type, Product type and Brand:
id_product_brand and name_name.

I did the select uniting the two tables to show me the result of the two:

<select name="carne">
    <option value="vazio">
   </option> 
<?php 
$sql = "(select tipo_produto from produto_tipo) 
  union (select nome_marca from produto_marca)";
$result = mysqli_query($conexao, $sql);
while($linha = mysqli_fetch_assoc($result)){ ?>
 <option value=" <?php echo $linha['tipo_produto'].$linha['nome_marca']; ?> ">
<?php echo utf8_decode($linha['tipo_produto'].$linha['nome_marca']); ?>
</option> 
<?php } ?>  
</select>

So far so good, but it is not showing the concatenated result. It is showing so:

arroz
tio jõao
urbano

I need you to show it like this:

arroz  tio joão
arroz  urbano

The result of the first part (arroz) comes from a table and the result of the second part (tio joao) comes from the second table.

  • You want to change only the text that appears to the user or the value as well?

  • only the output text

  • 1

    It happens because you’re doing it union select. Use join. Not answered because it is without information about the relationship between the tables. Post the structure of both tables.

  • As far as I searched with Join I would need something that connects the two tables, and they are totally independent

  • 2

    Then you have nothing to do. rsrs.. How will you know that brand A, B or C belongs to product X or product Y? You have to have something that relates them, right. rsrsr

  • If you are independent do a cross Join... but this can get a bit giant :)

  • Put the structure of the two tables and the result you expect them to bring. Just one detail UNION is not used like this, it joins two "equal" tables but will be different lines.

Show 2 more comments

1 answer

4


The problem is inUNION and in modeling

The UNION server to join distinct results of 2 or more SELECTS which may be totally independent, and these results will be listed in different lines just having the same number of columns with the same data type respectively !

If you want your query to return in the same row the result of a relation you can use the WHERE(obsolete) or JOIN

Considering the information you passed on, there is no table PRODUCT and your wish is that a product type has multiple brands and a brand has multiple product types forming a relationship MANY to many

In this scenario it is necessary to create a third table only to make the relationship between the 2 and the code would look like:

    Select tipo_produto , nome_marca FROM produto_tipo INNER JOIN 
TabelaDeRelacao ON produto_tipo.id_produto_tipo = TabelaDeRelacao.FK_Produto_Tipo INNER 
JOIN  produto_marca on produto_marca.id_produto_marca = TabelaDeRelacao.fk_produto_marca

But your example suggests that either the name product-type is not very appropriate for your table and should be product , have a third field fk_product_brand with reference to table production_brand and generate a relationship ONE to many ( a brand has several products and a product has a single brand )

And in that case the solution would be :

   Select tipo_produto , nome_marca FROM produto_tipo INNER JOIN 
produto_marca ON produto_tipo.fk_produto_marca = produto_marca.id_produto_marca

Or there must be a third table Produto( which has for example , name, fk_product_type , fk_product_brand ) forming 2 relationships ONE to many

 select tipo_produto , nome_marca from Produto 
inner join produto_tipo on fk_produto_tipo = id_produto_tipo
inner join produto_marca on fk_produto_marca = id_produto_marca 
  • 1

    I wanted the part of the many for many even, I created a table of relation and followed the example he gave, it worked. Obrogada

  • Hello john, I updated your question, because the word deprecated have another sense, the closest to deprecated in Portuguese would be obsolete or discontinued, however I did not understand one thing in your reply, where you heard that WHERE is deprecated?

Browser other questions tagged

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