473,409 Members | 1,952 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,409 software developers and data experts.

Trouble with query

I'm wondering if someone might be able to help me with a query in mysql.

I have 3 tables that contain the information I need.

Table 1
Id1
.
.
.

Table 2
Id2
Id1(FK)
.
.
.

Table 3
Id3
Id2(FK)
.
.
.

I want to pull all the information from these tables if table 2 has more than one of the same Id1(FK) [relation to Table1]

So far I have:
SELECT * FROM table2 INNER JOIN table1 ON (table2.Id1 = table1.Id1)
INNER JOIN table3 ON (table2.Id2 = table3.Id2)
GROUP BY table1.Id1
HAVING COUNT(table1.Id1) > 1

This works fine if I have data in table 3, but the relation is 0-many for table 2 & 3, so I might not have anything in table 3 relating to table 2 and if that's the case I don't get any results. Do I need to use some kind of left or right outer join? I've messed around with these too with no luck. I'm not as good at SQL as I'd like to be so I'm kind of stuck as to what to try now. Any help would be appreciated. Please let me know if more information is required and I can post the acutal tables.
Apr 25 '07 #1
1 1052
Hi,

May be this query will help you,

SELECT * FROM table2 INNER JOIN table1 ON (table2.Id1 = table1.Id1)
LEFT OUTER JOIN table3 ON (table2.Id2 = table3.Id2)
GROUP BY table1.Id1
HAVING COUNT(table1.Id1) > 1;

Thanks,
Scarlet


I'm wondering if someone might be able to help me with a query in mysql.

I have 3 tables that contain the information I need.

Table 1
Id1
.
.
.

Table 2
Id2
Id1(FK)
.
.
.

Table 3
Id3
Id2(FK)
.
.
.

I want to pull all the information from these tables if table 2 has more than one of the same Id1(FK) [relation to Table1]

So far I have:
SELECT * FROM table2 INNER JOIN table1 ON (table2.Id1 = table1.Id1)
INNER JOIN table3 ON (table2.Id2 = table3.Id2)
GROUP BY table1.Id1
HAVING COUNT(table1.Id1) > 1

This works fine if I have data in table 3, but the relation is 0-many for table 2 & 3, so I might not have anything in table 3 relating to table 2 and if that's the case I don't get any results. Do I need to use some kind of left or right outer join? I've messed around with these too with no luck. I'm not as good at SQL as I'd like to be so I'm kind of stuck as to what to try now. Any help would be appreciated. Please let me know if more information is required and I can post the acutal tables.
Apr 27 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: fstenoughsnoopy | last post by:
Ok the complete story. I have a Contact Table, Query and Form, that are used to input and store the contact info for customers. They have FirstName, LastName and Address as the primary key...
0
by: harry12 | last post by:
Hello- I'm fairly new at using Microsoft Access and I'm having trouble getting a couple of things to work in my database. The first is that I have yet to find a way to get an append query to...
1
by: ferraro.joseph | last post by:
Hi, I'm querying Salesforce.com via their AJAX toolkit and outputting query results into a table. Currently, their toolkit does not possess the ability to do table joins via their structured...
11
by: kennthompson | last post by:
Trouble passing mysql table name in php. If I use an existing table name already defined everything works fine as the following script illustrates. <?php function fms_get_info() { $result =...
5
by: tschulken | last post by:
I have a query where i need to look for a value of a lower level xml element based on the value of a parent element existing first. Here is a simple example of the xml <S3Client> <Buttons>...
3
by: narpet | last post by:
Hello all... I'm having trouble with a query. Here's the situation: I have two tables, one that holds client info and another that holds invoicing info. The two tables have client number in...
1
by: =?iso-8859-1?B?RXJpYyBM83Bleg==?= | last post by:
Hello Guys! I just installed MySQL Community Server 5.0 onto my computer, which is running Red Hat 8.0 and it works fine, but I was trying to install the MySQL GUI Tools (Query Browser, MySQL...
1
by: nico3334 | last post by:
I'm having trouble with a query using "MAX". In my query I'm trying to use 2 date columns as my criteria. Here is the format for each column: Date1: 01/01/2007 Date2: 200701 For my...
3
by: lostdawg | last post by:
Hi, I am having trouble with the following query. I need to sort from a list of contacts the last date each was contacted. This is to be represented in days so for instance: 0-42 days...
1
by: George | last post by:
I have just loaded Access 2007 and am having trouble creating a new database where I update my table from a query. I have done this numerous times in years past but am totally frustrated tryng to...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
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...
0
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.