473,660 Members | 2,475 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

All Records From Table A - All Records From Table B - Join Alike Records

I am by no means a SQl Jedi as will be apparent by my question, but I
can usually figure out a select statement on my own. I have one today
though that really has me stumped. I am working in MySQlL 5.

In My first select statement I get all my records from Table B
SELECT
`table_A`.`ITEM `,
`table_A`.`DECS CRIPTION`,
`table_A`.`UM`,
`table_A`.`PHAS E`,
`table_B`.`Qty` ,
`table_B`.`Calc ` as calculated
FROM
`table_A`
Inner Join `table_B` ON `req_itemlist`. `ITEM` = `table_B`.`Item ID`

In my second statement I get my records that match in this case phase
401 in Table B and all my Table A records for phase 401.

SELECT
`table_A`.`ITEM `,
`table_A`.`DECS CRIPTION`,
`table_A`.`UM`,
`table_A`.`PHAS E`,
`table_B`.`Qty` ,
`table_B`.`Calc ` as calculated
FROM
`table_A`
Left Outer Join `table_B` ON `req_itemlist`. `ITEM` = `table_B`.`Item ID`
Where
table_A.PHASE In ('401' )

Now I need to combine the Data of both recordsets. I need EVERYTHING in
Table B, but I also need All Table A records that match the phase
selection....

Can I write this one query or do I need to use a Temp table?

Oct 3 '06 #1
2 3252
The UNION clause can be used to join the results of two queries.

http://dev.mysql.com/doc/refman/5.1/en/union.html

The basic syntax is..

CREATE TABLE test1 (id int, something varchar(50));
CREATE TABLE test2 (id int, something varchar(50));

INSERT INTO test1 (id, something) VALUES (1, 'hello'),(2, 'world');
INSERT INTO test2 (id, something) VALUES (1, 'foo'),(2, 'bar');

SELECT something, id FROM test1
UNION
SELECT something, id FROM test2;

kevinjbowman wrote:
I am by no means a SQl Jedi as will be apparent by my question, but I
can usually figure out a select statement on my own. I have one today
though that really has me stumped. I am working in MySQlL 5.

In My first select statement I get all my records from Table B
SELECT
`table_A`.`ITEM `,
`table_A`.`DECS CRIPTION`,
`table_A`.`UM`,
`table_A`.`PHAS E`,
`table_B`.`Qty` ,
`table_B`.`Calc ` as calculated
FROM
`table_A`
Inner Join `table_B` ON `req_itemlist`. `ITEM` = `table_B`.`Item ID`

In my second statement I get my records that match in this case phase
401 in Table B and all my Table A records for phase 401.

SELECT
`table_A`.`ITEM `,
`table_A`.`DECS CRIPTION`,
`table_A`.`UM`,
`table_A`.`PHAS E`,
`table_B`.`Qty` ,
`table_B`.`Calc ` as calculated
FROM
`table_A`
Left Outer Join `table_B` ON `req_itemlist`. `ITEM` = `table_B`.`Item ID`
Where
table_A.PHASE In ('401' )

Now I need to combine the Data of both recordsets. I need EVERYTHING in
Table B, but I also need All Table A records that match the phase
selection....

Can I write this one query or do I need to use a Temp table?
Oct 3 '06 #2
kevinjbowman wrote:
I am by no means a SQl Jedi as will be apparent by my question, but I
can usually figure out a select statement on my own. I have one today
though that really has me stumped. I am working in MySQlL 5.

In My first select statement I get all my records from Table B
SELECT
`table_A`.`ITEM `,
`table_A`.`DECS CRIPTION`,
`table_A`.`UM`,
`table_A`.`PHAS E`,
`table_B`.`Qty` ,
`table_B`.`Calc ` as calculated
FROM
`table_A`
Inner Join `table_B` ON `req_itemlist`. `ITEM` = `table_B`.`Item ID`

In my second statement I get my records that match in this case phase
401 in Table B and all my Table A records for phase 401.

SELECT
`table_A`.`ITEM `,
`table_A`.`DECS CRIPTION`,
`table_A`.`UM`,
`table_A`.`PHAS E`,
`table_B`.`Qty` ,
`table_B`.`Calc ` as calculated
FROM
`table_A`
Left Outer Join `table_B` ON `req_itemlist`. `ITEM` = `table_B`.`Item ID`
Where
table_A.PHASE In ('401' )

Now I need to combine the Data of both recordsets. I need EVERYTHING in
Table B, but I also need All Table A records that match the phase
selection....

Can I write this one query or do I need to use a Temp table?

You can use UNION but you have to make sure your field counts match.
Oct 4 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2101
by: Edward | last post by:
SQL Server 7.0 If I run the following in Query Analyzer I get no records returned: exec GetLeadsOutcome_Dealer '1/1/2003','12/2/2003',10, '176, 183' If, however, I run either : exec GetLeadsOutcome_Dealer '1/1/2003','12/2/2003',10, '176'
2
1978
by: AG | last post by:
I have a very big table with 20 million records DistinctProjectionKey which i join several times to different tables in this query. select distinct distinctprojectionkeyid,dpk.MarketID, dpk.Classificationid, dpk.DistributorID, dpk.ManufacturerID, dpk.LocationID, dpk.TimeID,P4.FACTOR as factor1,P3.FACTOR as factor2 ,P2.FACTOR as factor3,P1.FACTOR as factor4
8
4322
by: tom | last post by:
I am new to SQL administration. >From a list of IDs that are the primary key in one table (i.e. Customer Table), I want to make changes in tables that use those IDs as a foreign key. Basically I want to say: If fk_ID is in list then do these statements to that record
6
8153
by: Sven Pran | last post by:
Probably the answer is there just in front of me only awaiting me to discover it, but: 1: I want to build a query that returns all records in one table for which there is no successful "join" in another table but I have not found what the field criteria should look like? 2: And if/when I succeed I should further like to build a new record (with
15
2712
by: Hi5 | last post by:
Hi, I am designing a database for a client in which It has a client table including the followings: 1-Table Client 2-Table lookupcategory 3-Table Ctegory
12
12249
by: jaYPee | last post by:
I have currently using a dataset to access my data from sql server 2000. The dataset contains 3 tables that is related to each other. parent/child/grandchild relationship. My problem is it's very slow to retrieve records from sql server. I'm using sqldataadapter.fill method to populate dataset. Parent table contains more than 3,000 records and child table contains more than 10,000 records and grandchild table contains more than 90,000...
3
6032
by: Cindy | last post by:
I'm trying to use the NEWID function in dynamic SQL and get an error message Incorrect syntax near the keyword 'ORDER'. Looks like I can't do an insert with an Order by clause. Here's the code: SELECT @SQLString = N'INSERT INTO TMP_UR_Randoms(Admit_DOCID, Client_ID, SelectDate, SelectType,RecordChosen)' SELECT @SQLString = @SQLString + N'(SELECT TOP ' + @RequFilesST + ' Admit_DOCID, Client_ID, SelectDate, SelectType, RecordChosen...
1
2765
by: babamc4 | last post by:
Hi All, I am trying to run a query to include null fields. My tables are as follows: Table Demographics 54 records (Main table) Table Follow Up During Radiotherapy 100+ records (Sub table) Table Follow Up Post RT 100+ (Sub table) Table Last Contact 54 records (Sub table) All are one to many except for Table Last Contact which is one to one.
1
3413
by: bluereign | last post by:
Thank you for your assistance. I am a novice looking to JOIN and append or combine records from 2 current Tables into 2 new Tables named below. I have been able to JOIN Tables with the script below. My problem is if I don’t use the WHERE clause in my script below, the script can query up to 3 records with the same “LoanNo” due to the fact that there can be up to 3 “TypeCodes” in each Table (Borrower, Co-Borrower 1 and Co-Borrower 2...
0
8428
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8341
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8539
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8630
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7360
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4176
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4342
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2759
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1982
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.