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

[SQL] need help on a mysql query

hello

I need help building a SELECT query... well, two queries actually

i have three tables:

products / fields id, name, type, country
producttypes / fields: id, name
countries / fields: id, name

as you probably guessed, products.type stores a value from
producttypes.id and products.country stores a value from countries.id

now, in my first query i need to select all producttypes that have
more than 0 products belonging to that category.

in a subsequent query i'll have to select all countries that have more
than 0 products which belong to a given producttype.

Thanks for taking the time to help me solve this one..

somaBoy MX

Nov 21 '07 #1
4 1471
SBmx wrote:
hello
Hi,

Not excactly a PHP question, but here we go.
>
I need help building a SELECT query... well, two queries actually

i have three tables:

products / fields id, name, type, country
producttypes / fields: id, name
countries / fields: id, name

as you probably guessed, products.type stores a value from
producttypes.id and products.country stores a value from countries.id
Nope, I wouldn't have guessed that.
I always give columns the same name if I create a Foreign Key constraint.
But clear now. :-)
>
now, in my first query i need to select all producttypes that have
more than 0 products belonging to that category.
Try using GROUP BY and use HAVING
>
in a subsequent query i'll have to select all countries that have more
than 0 products which belong to a given producttype.
Same: GROUP BY and HAVING.
>
Thanks for taking the time to help me solve this one..
You'll have to solve it yourself, but you know what to look for now. ;-)
>
somaBoy MX
Good luck.

Regards,
Erwin Moller
Nov 21 '07 #2
On Nov 21, 5:05 pm, Erwin Moller
<Since_humans_read_this_I_am_spammed_too_m...@spam yourself.comwrote:
>
Try using GROUP BY and use HAVING

actually, i looked into subqueries and came up with this:
SELECT name, id FROM countries WHERE id IN (SELECT country FROM
products)
SELECT name, id FROM producttypes WHERE id IN (SELECT type FROM
products WHERE country=$intCountry)

seems to work, but your solution would probably be more backwards
compatible with older mysql versions.

thanks a bunch!

somBoy MX
Nov 21 '07 #3
SBmx wrote:
On Nov 21, 5:05 pm, Erwin Moller
<Since_humans_read_this_I_am_spammed_too_m...@spam yourself.comwrote:
>Try using GROUP BY and use HAVING


actually, i looked into subqueries and came up with this:
SELECT name, id FROM countries WHERE id IN (SELECT country FROM
products)
SELECT name, id FROM producttypes WHERE id IN (SELECT type FROM
products WHERE country=$intCountry)
Yes, that works in this situation.
But what will you do when you need all producttypes that have more than,
say, 2 or 3 products belonging to a certain category?

Have a look at GROUP BY and HAVING.
They are designed for this. ;-)
seems to work, but your solution would probably be more backwards
compatible with older mysql versions.
I don't know much about mySQL.
Last time I looked into it I ran away screaming (back to PostgreSQL).
I prefer PostgreSQL, as superior database compared to mySQL in my humble
opinion. (MySQL is a little faster, but that is all)

But I expect that mySQL can handle both approaches (subselect and GROUP BY).

>
thanks a bunch!
You are welcome.
Good luck

Regards,
Erwin Moller
>
somBoy MX
Nov 21 '07 #4
SBmx wrote:
hello

I need help building a SELECT query... well, two queries actually

i have three tables:

products / fields id, name, type, country
producttypes / fields: id, name
countries / fields: id, name

as you probably guessed, products.type stores a value from
producttypes.id and products.country stores a value from countries.id

now, in my first query i need to select all producttypes that have
more than 0 products belonging to that category.

in a subsequent query i'll have to select all countries that have more
than 0 products which belong to a given producttype.

Thanks for taking the time to help me solve this one..

somaBoy MX

You can get more help for MySQL questions in a MySQL newsgroup such as
comp.databases.mysql.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Nov 21 '07 #5

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

Similar topics

8
by: markus | last post by:
This string does not work in php: $sql="UPDATE mytable SET myfield=myuserfunction(myfield)" What I want is my function to evaluate each field and put the new value in the field. For...
3
by: cooldv | last post by:
i am running a website on Windows 2000 server with ASP 3 webpages and Access 2000 database. (with a hosting company) traffic is slow at this time but expect to grow. lately i have been reading...
2
by: Jason Tudisco | last post by:
Hello, I not sure if this is the right place to ask this... I am using mysql. What I need is a SQL statement that can find what years are in the database that is greater than last year. For...
6
by: Hans | last post by:
Hello group, I have a table with the next contents. It lists data about : Who sent what kind of message at what time. For the Level column: The highest level is Critical, the middle is Warning,...
4
by: DBNovice | last post by:
I have a database that keeps records on the issue and failure of an item. Currently, the database is poorly desisned; therefore I'm performing queries to break the data into normalized tables and...
2
by: jet | last post by:
Hi, Maybe this is an easy task, but I'm having a really hard time figuring out how to do this. I'm a complete newbie to SQL Server. I have a database dump file from MySQL that's in .sql...
4
by: Dave | last post by:
I have a MS sql 2000 db that needs to sync with a Mysql db. When a password is changed in the MS SQL table, I need to make sure that the same value is updated in the Mysql db. I dont have any...
8
by: Fred | last post by:
Hello, Our website is currently developed in ASP/Mysql 4. The dedicated servers on which it is currently hosted arrive at saturation. Here is their configuration: - 1 server PIV 2,8Ghz 1GB...
0
Coldfire
by: Coldfire | last post by:
Since i cannot show the differences in a two-column like table. I am first putting MS SQL Server 2005 and then MySQL 5.x. MS SQL Server 2005 Brief Overview - SQL Server is a full-fledged...
2
by: Sudhakar | last post by:
A) validating username in php as part of a registration form a user fills there desired username and this is stored in a mysql. there are certain conditions for the username. a) the username...
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
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?
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
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...
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
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.