How to generate a query which consider data from one table and correspondingly fill 0 for non matching fields in mysql

anishantony

I am having 2 tables. t1 and t2.

 create table t1  
(   
r1 int,   
n varchar(10)   
);   

create table t2   
(   
r2 int,   
m int  
)   

the way I populate t1 is

r1--- n    
.----------    
1--- abc

and t2 is having the following data

r2--- m  
.----------  
1---      46  
2---      50   
3---      100

I want to display all r2 values for corresponding r1.
Also mismatching things should also display but with 0.
I wrote a outer join query like this

select * from  
t1 right join  
t2  
on t1.r1 = t2.r2  

The ouput I am getting here is

r1------n------r2------m   
.----------------------   
1------   abc---  1--- 45  
null--- null--- 2--- 60  
null--- null--- 3--- 100  

But what I am expecting is a output like this

r1----n----r2----m   
.----------------------   
1---   abc---  1--- 45  
1--- abc--- 2--- 0  
1--- abc--- 3--- 0  

Could anyone propose a solution to achieve this.
If I adds one more row into t1, say 2---xyz
then the output should be

r1----n----r2----m   
.----------------------   
1---   abc---  1--- 45  
1--- abc--- 2--- 0  
1--- abc--- 3--- 0  
2---xyz---1---0  
2---xyz---2---60  
2---xyz---3---0  

Your help would be much appreciated.

Regards
Anish Antony

GarethD

I think you want to CROSS JOIN your tables:

SELECT  t1.r1, 
        t1.n, 
        t2.r2, 
        CASE WHEN t1.r1 = t2.r2 THEN t2.m ELSE 0 END AS m
FROM    t1
        CROSS JOIN t2
ORDER BY t1.r1, t2.r2;

Example on SQL Fiddle

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to generate a query which consider data from one table and correspondingly fill 0 for non matching fields in mysql

From Dev

MySQL Join; Query only non-matching table fields

From Dev

Query to select related data from two tables in which one table has no related fields in third table

From Dev

How to fill into more than one textbox with data taken from mysql table after dropdown selection

From Dev

How to fill into more than one textbox with data taken from mysql table after dropdown selection

From Dev

How to select non-matching rows from two mysql table

From Dev

MySQL copy data from one table to another in different fields

From Dev

How to move one fields data from another field within same mysql database table?

From Dev

How to insert data from 3 different input fields together in one column of table in mysql database in codeigniter

From Dev

How to query rows with one matching and one non-matching field

From Dev

How to find duplicates in a large table based on matching and non matching fields?

From Dev

How do I generate several reports from one MySQL query?

From Dev

SQL - Fill one table from fields of another table

From Dev

SQL - Fill one table from fields of another table

From Dev

Delete non-matching data from table

From Dev

SQL Query for 2 tables how to fill fields from first to second table

From Dev

MySQL: Use data from one table to fill a second table using phpMyAdmin

From Dev

MySQL: Use data from one table to fill a second table using phpMyAdmin

From Dev

How to get records from one table which are not in another table's fields (3)

From Dev

Validate fields from one table to another in MySQL

From Dev

How to Replace and Update Data From One Table to Another Table in MySQL

From Dev

How to Replace and Update Data From One Table to Another Table in MySQL

From Dev

How to fill input fields in form with data from row in html table I want to edit

From Dev

mysql query combine table from one table

From Dev

mysql query combine table from one table

From Dev

How do I insert a column of values from one table to another, non-matching schemas?

From Dev

How can I make a Hive table from a .csv file which has one column with fields delimiited by semicolon ;

From Dev

mysql return matching other rows when one table returns 0

From Dev

How to fetch multiple data from mysql database with only one query?

Related Related

  1. 1

    How to generate a query which consider data from one table and correspondingly fill 0 for non matching fields in mysql

  2. 2

    MySQL Join; Query only non-matching table fields

  3. 3

    Query to select related data from two tables in which one table has no related fields in third table

  4. 4

    How to fill into more than one textbox with data taken from mysql table after dropdown selection

  5. 5

    How to fill into more than one textbox with data taken from mysql table after dropdown selection

  6. 6

    How to select non-matching rows from two mysql table

  7. 7

    MySQL copy data from one table to another in different fields

  8. 8

    How to move one fields data from another field within same mysql database table?

  9. 9

    How to insert data from 3 different input fields together in one column of table in mysql database in codeigniter

  10. 10

    How to query rows with one matching and one non-matching field

  11. 11

    How to find duplicates in a large table based on matching and non matching fields?

  12. 12

    How do I generate several reports from one MySQL query?

  13. 13

    SQL - Fill one table from fields of another table

  14. 14

    SQL - Fill one table from fields of another table

  15. 15

    Delete non-matching data from table

  16. 16

    SQL Query for 2 tables how to fill fields from first to second table

  17. 17

    MySQL: Use data from one table to fill a second table using phpMyAdmin

  18. 18

    MySQL: Use data from one table to fill a second table using phpMyAdmin

  19. 19

    How to get records from one table which are not in another table's fields (3)

  20. 20

    Validate fields from one table to another in MySQL

  21. 21

    How to Replace and Update Data From One Table to Another Table in MySQL

  22. 22

    How to Replace and Update Data From One Table to Another Table in MySQL

  23. 23

    How to fill input fields in form with data from row in html table I want to edit

  24. 24

    mysql query combine table from one table

  25. 25

    mysql query combine table from one table

  26. 26

    How do I insert a column of values from one table to another, non-matching schemas?

  27. 27

    How can I make a Hive table from a .csv file which has one column with fields delimiited by semicolon ;

  28. 28

    mysql return matching other rows when one table returns 0

  29. 29

    How to fetch multiple data from mysql database with only one query?

HotTag

Archive