84844

Fetch data from two different tables in one query

Question:

On my webserver there is a databse with following two tables:

tbl_Friend tbl_Colleague | id | Name | First name | Place | | id | Name | First name | Place | ---------------------------------- ---------------------------------- | 1 | XXXX | XXXXXXXXXX | 1 | | 1 | AAAA | AAAAAAAAAA | 1 | | 2 | YYYY | YYYYYYYYYY | 2 | | 2 | BBBB | BBBBBBBBBB | 3 | | 3 | ZZZZ | ZZZZZZZZZZ | 1 | | 3 | CCCC | CCCCCCCCCC | 4 |

Now I want to fetch all Persons from tbl_Friend and tbl_Colleague which life in place 1. For that I have to the data from both tables and here is my problem: How can I fetch data from two different tables in only one query? My result should look like this:

| id | Name | First name | Place | ---------------------------------- | 1 | XXXX | XXXXXXXXXX | 1 | | 1 | AAAA | AAAAAAAAAA | 1 | | 3 | ZZZZ | ZZZZZZZZZZ | 1 |

Can I use something like FROM tbl_Friend | tbl_Colleague or something else? Or do I have to use a Join for this?

Answer1:

Try this:

SELECT id, Name, First name, Place FROM tbl_Friend WHERE Place= 1 UNION ALL SELECT id, Name, First name, Place FROM tbl_Colleague WHERE Place= 1

Answer2:

try this:

select * from tbl_friend a, tbl_colleague b where a.place = b.place and place like '1';

Recommend

  • Why is Elixir's Enum.count not returning a number? [duplicate]
  • Creating a Dictionary Composite Key out of Every Permutation in a String[] within a String[][]
  • Writing to a text file from DataGridView in C# windows form
  • select dynamic column value from a rowtype variable in pl sql
  • “Unexpected indentation” with restructuredtext list
  • Looking for a way to have a base36 sequence in C#
  • video tag html5 with src from base64_encode not work on different device and browser
  • wsHttpBinding on the wcf service and web reference on the client don't work
  • Nested text links in HTML
  • GCM push notification with C# asp.net giving null as notification in android devices
  • How to add a custom HTTP header to ajax request with js or jQuery
  • Some floating point precision and numeric limits question
  • How does a browser handle cookie with no path and no domain
  • SQL duplicate rows with multiple left joins
  • jQuery - how to validate a date of birth using jQuery Validation plugin?
  • Creating Setup of large data with NSIS Script
  • A simple datepicker in VueJS
  • How to parsing NSDate to RFC 822 always use in English?
  • stop execution in Custom validator if it false
  • Open Existing DB in MySQL WorkBench
  • User messaging system
  • How to 'create temp table as select' in Slick?
  • How to override value that appears in a dropdown in the rails_admin gem
  • SQL Query - Table Joining Problems
  • Bigquery event streaming and table creation
  • How to return DataSet (xsd) in WCF
  • Low TTL with Leveled Compaction, should I reduce gc_grace_seconds to improve read performance withou
  • close() was never explicitly called on database
  • Eliminate partial duplicate rows from result set
  • How to assign byte[] as a pointer in C#
  • Unable to get column index with table.getColumn method using custom table Model
  • How can I extract results of aggregate queries in slick?
  • Word Open XML Mail Merge
  • Replace value with Factor in r data.table
  • Database structure design with variable amounts of fields
  • Submit form in a displaytag pagination
  • Delete MySQLi record without showing the id in the URL
  • Unanticipated behavior
  • Comma separated Values
  • Trying to get generic when generic is not available