Angel Valdez
Angel Valdez

Reputation: 87

obtain results of two different tables mysql

I have two tables.

Table stock

fecha       | tarifa

2017-01-06    500 
2017-01-07    500 
2017-01-08    500 

Table promotions

fecha      |  percent

2017-01-07    0.10
2017-01-08    0.10
2017-01-07    0.15
2017-01-08    0.15

and i need to obtain while mysql query this:

fecha       | tarifa  | percent

2017-01-06    500      0
2017-01-07    500      0.10
2017-01-08    500      0.10
2017-01-06    500      0
2017-01-07    500      0.15
2017-01-08    500      0.15

Thank you!!!

Update

i need that create this array with the result of the query.

Array
(
    [1] => Array
        (
            [0] => Array
                (
                    [fecha] => 2017-01-06
                    [tstandard]=>500
                    [porcentaje] => 0.00
                )

            [1] => Array
                (
                    [fecha] => 2017-01-07
                    [tstandard]=>500
                    [porcentaje] => 0.10
                )

            [2] => Array
                (
                    [fecha] => 2017-01-08
                    [tstandard]=>500
                    [porcentaje] => 0.10
                )
        )

    [2] => Array
        (
            [0] => Array
                (
                    [fecha] => 2017-01-06
                    [tstandard]=>500
                    [porcentaje] => 0.00
                )

            [1] => Array
                (
                    [fecha] => 2017-01-07
                    [tstandard]=>500
                    [porcentaje] => 0.15
                )

            [2] => Array
                (
                    [fecha] => 2017-01-08
                    [tstandard]=>500
                    [porcentaje] => 0.15

                )
        )
)

I have done the array but without the tstandard of each day

Upvotes: 0

Views: 73

Answers (1)

Darshan Mehta
Darshan Mehta

Reputation: 30819

You can use left outer join to get the desired output, e.g.:

SELECT s.fecha, s.tarifa, IFNULL(p.percent, 0)
FROM stock s LEFT JOIN promotions p ON s.fecha = p.fecha;

Here's the SQL Fiddle.

Update

If you want to restrict the output with dates, you can use the below query:

SELECT s.fecha, s.tarifa, IFNULL(p.percent, 0)
FROM stock s LEFT JOIN promotions p ON s.fecha = p.fecha
WHERE s.fecha BETWEEN $checkin AND $checkout;

Upvotes: 1

Related Questions