Jump to content

Problem with Select Union


leerobbo

Recommended Posts

Hello all

 

I'm attempting to retrieve data from two tables and while it is working to an extent, a number of rows of data fail to display. An example statement I've been using is ' SELECT home, away, ftr FROM eng1011 WHERE home = 'Liverpool' OR away = 'Liverpool' UNION SELECT home, away, ftr FROM eng0910 WHERE home = 'Liverpool' OR away = 'Liverpool';

 

That would display 67 rows of data but the problem is there should be 76 rows of data. The rows of data missing are from the second table and if the tables are swapped around so 0910 table is the first select statement then the 1011 table would have the rows of data missing.

 

I don't really know what should be done here so any help is appreciated thanks.

Link to comment
Share on other sites

What I commonly do when doing a union is add another column, and fill it with what table the row came from, not only does it give all of the rows, but now you have information on the table.

 

select column_from_table_1 as col1, 'table1' as table from table_1 where something = 'some value'
union
select column_from_table_2 as col1, 'table2' as table from table_2 where something = 'some value'

 

Note: I am not sure if this is a better solution than UNION ALL (probably not), but it is helpful IMO.

Link to comment
Share on other sites

That has absolutely nothing do with with the "missing" rows -- they're missing because they're not distinct.  MySQL thinks it's doing you a favour.  That will indirectly make them non-distinct -- but that's not robust.

 

Yes it does, they are usually no longer distinct if you add that column

 

Not Distinct:

1, table1

1, table2

 

removing what table they came from make them distinct

Link to comment
Share on other sites

That has absolutely nothing do with with the "missing" rows -- they're missing because they're not distinct.  MySQL thinks it's doing you a favour.  That will indirectly make them non-distinct -- but that's not robust.

 

Yes it does, they are usually no longer distinct if you add that column

 

Not Distinct:

1, table1

1, table2

 

removing what table they came from make them distinct

You're missing the point --- MySQL is still running DISTINCT, and wasting it's time.  The fact that you might want to know what table they originated from is not the issue.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.