Display result of different tables with similar data - MYSQL

Asked

Viewed 121 times

-1

I have never done this, I need to create a QUERY for MYSQL to display data from two tables, a table of the male gender and another of the female gender. I know that the correct thing is to create a field to differentiate the gender, but the example below is only didactic, because what I need is for another data structure, where the example portrays well what I need to do.

tbl_masc | | id (int(11)) | name (varchar(50)) | surname (varchar(50)) | age (int(3)) | data_nasc (datetime())

tbl_fem | | id (int(11)) | name (varchar(50)) | surname (varchar(50)) | age (int(3)) | data_nasc (datetime())

I know tables relationship as follows:

<?php
$SQL = "SELECT 
            a.*,b.*,
            (
            SELECT 
                `cor` 
            FROM 
                `cabelo` 
            WHERE 
                `id_cor` = a.`cor_cabelo` 
            LIMIT 
                1
            ) AS `cabelo` 
        FROM 
            `tbl1` a 
        LEFT JOIN 
            `tbl2` b 
        ON 
            b.`id` = b.`id_pai` 
        WHERE 
            a.`status` = 'A' 
        ORDER BY 
            a.`nome` 
                ASC 
        LIMIT 
            12
        ;";
?>

The intention of the above query is to let you know that I know how to mount a query, perform queries and whatever is necessary within my limitation, because I’m not asking for help with something chewed, is that I really don’t know how to do. I need a north for that!

  • Do you want to create a loop in PHP or do a query that searches the data in the 2 tables? It was not clear the question.

  • Sorry, I corrected

  • I still don’t understand... you want to select the data from two different tables is that it? You can do this only with a select, without having to loop any... if you put part of your PHP code, maybe it is clearer...

  • That’s right, QUERY SELECT for MYSQL rs

1 answer

0

To select data from more than one table, just use one SELECT with multiple tables. Technically this would be a JOIN but without any comparison clause between them. This would return all records of the 2 tables according to the WHERE.

In the example you gave, the query to return all data from the 2 tables would be:

SELECT * FROM tbl_masc, tbl_fem;

You can then process the data in PHP or limit in the query with WHERE, LIMIT, etc..

  • I will try it, you need to use it if you have to perform subquery’s right, because it has some relationship dependencies with data of second importance.

  • If you have any dependency there changes the query, because you will need to do a JOIN, more like what you put in the question. But as in your example the selection is two tables unrelated between them and only with equal columns, do not need to JOIN (it is implicit, as if it were JOIN 1=1)

  • I didn’t, because if there are two fields with the same name, I would have to create a nickname for it, and then it wouldn’t be recovered in the looping, because I would need to reuse the same value of the array for both, so I couldn’t without having to use some conditions.

  • So the example you gave is incomplete. You need a JOIN to select the fields that are equal in both. In your example it would be like finding men and women with the same date of birth for example?

  • No beast, I believe that not read until the end after the example, what I did was an example that I do not want chewed because I know a bit of sql, because it is usually asked a question here in general if you think you want chewed, and it is not necessary, it is even limitation.

  • I’m referring to the table you gave of example... anyway, without more details about the tables and the query becomes difficult to help...

Show 1 more comment

Browser other questions tagged

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