473,396 Members | 2,061 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,396 software developers and data experts.

A simple (?) SQL query for someone clever

Unfortunately I can't use the excuse of being a SQL newbie, but drawn a
blank on the following "simple" problem.
I have a table containing the following (extract) :

+----+-----------+------------+---------+
| id | fk_MainID | fk_GroupID | Visible |
+----+-----------+------------+---------+
| 4 | 158 | 1 | 1 |
| 3 | 148 | 1 | 1 |
| 5 | 150 | 1 | 1 |
| 6 | 146 | 1 | 1 |
| 7 | 127 | 1 | 1 |
| 8 | 125 | 1 | 1 |
| 9 | 117 | 1 | 1 |
| 10 | 119 | 1 | 1 |
| 11 | 128 | 1 | 1 |
| 12 | 118 | 1 | 1 |
| 13 | 105 | 1 | 1 |
| 14 | 99 | 1 | 1 |
| 15 | 102 | 1 | 1 |
| 16 | 153 | 1 | 1 |
| 17 | 157 | 1 | 1 |
| 18 | 152 | 1 | 1 |
| 19 | 149 | 1 | 1 |
| 28 | 162 | 10 | 0 |
| 25 | 160 | 1 | 1 |
| 27 | 162 | 1 | 1 |
| 26 | 160 | 10 | 0 |
| 29 | 151 | 1 | 1 |
+----+-----------+------------+---------+

I need to find the "fk_MainID" where there is a "Visible=1" value, but NOT a
"Visible=0".
For example:
SELECT fk_MainID FROM GRP WHERE Visible=1
is no good because it will select fk_MainID=162, which also has a Visible=0
row.
I can do it with a sub-select, eg.
SELECT fk_MainID FROM GRP WHERE fk_MainID NOT IN (SELECT fk_MainID FROM GRP
WHERE Visible=0)
but unfortunately the query has to be "cross platform" at least to the
extent that it will also work on MySQL, which doesn't support sub-selects in
the release 3.x versions.

Very grateful in advance for any help.
Thanks
Jul 20 '05 #1
2 2042
Assuming that Visible is not nullable and contains only the values 0 and 1:

SELECT fk_mainid
FROM GRP
GROUP BY fk_mainid
HAVING MIN(visible)=1

--
David Portas
------------
Please reply only to the newsgroup
--
Jul 20 '05 #2
David,
You are a very clever man and your beer is in the mail.
I was trying all combinations of WHERE conditions, and never considered this
approach.
Thanks

Assuming that Visible is not nullable and contains only the values 0 and 1:
SELECT fk_mainid
FROM GRP
GROUP BY fk_mainid
HAVING MIN(visible)=1

--
David Portas
------------
Please reply only to the newsgroup
--

Jul 20 '05 #3

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

Similar topics

2
by: ensnare | last post by:
This query is running REAL slow ... like 1.2 secs ... any ideas on how I could optimize it? Perhaps my indexes are incorrect? $this->query = "SELECT m.username as username, e.title as title,...
2
by: Dan | last post by:
Hi all! I am creating a Disney "Today in History" database, and so far have about 300 entries in it. Well, my Access database is great, and my Frontpage form is great, but I want to include a...
1
by: j.mandala | last post by:
I created a simple link between two tables in a query. TableA has Social Security numbers stored as Long Integer Data. (I imported this table). The Join is between these two fields Table ...
3
by: Bob C. | last post by:
When I migrated my tables to SQL Server I needed a way to overcome the slow performance of the Find method on my recordsets. Although this can be done by accessing the table directly using dao and...
5
by: lis0122 | last post by:
I'm a database marketing manager for a small non-profit. I created a query that prompts you for a company name and then pulls a list of all of our contacts in the database in that company. Our...
51
by: Alan | last post by:
hi all, I want to define a constant length string, say 4 then in a function at some time, I want to set the string to a constant value, say a below is my code but it fails what is the correct...
3
by: John Baker | last post by:
Hi:7 Newby here to ASP, and using the ASP.NET Web Matrix development tool. While that tool looks great for a Newby, I have run into a snag. I have an HTML Text Box which I have named HireInput,...
27
by: one man army | last post by:
Hi All- I am new to PHP. I found FAQTS and the php manual. I am trying this sequence, but getting 'no zip string found:'... PHP Version 4.4.0 $doc = new DomDocument; $res =...
9
by: Alan Mailer | last post by:
Ok, my Access 2002 language writing skills are VERY rusty,. I would know how to do what I need using SQL Server's "Coalesce' function, but I don't have that available to me in the Access 2002...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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.