473,800 Members | 2,659 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

[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.countr y 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 1493
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.countr y 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_r ead_this_I_am_s pammed_too_m... @spamyourself.c omwrote:
>
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=$intCou ntry)

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_r ead_this_I_am_s pammed_too_m... @spamyourself.c omwrote:
>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=$intCou ntry)
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.countr y 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*******@attgl obal.net
=============== ===

Nov 21 '07 #5

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

Similar topics

8
2046
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 example, this works: $sql= "UPDATE mytable SET myfield=myfield+3";
3
3355
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 about sql database and sql server, specially this article: http://www.aspfaq.com/show.asp?id=2195 will someone help me understand: 1. with *SQL Server*, do i keep my current Access 2000 database and ASP pages?
2
26610
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 example... I have a table full of dates ranging from 2001 to 2005..
6
2054
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, the lowest is Info. Id Timestamp Level 1 12:00 Info 1 13:00 Warning
4
2979
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 attempting to perform a "left join" query to build a cross-reference table. The left join query is currently taking nearly 2 hours for MySQL to process, using Navicat as a front-end. My system specs are 1.4Mhz Pentium Processor with 1GB of RAM...
2
22715
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 format. I'm trying to figure out how to import that into SQL Server 2000 so that I'll be able to manipulate it in a gui format, rather than command line. I can't find any import that takes a .sql file. I've been trying to load it into the query...
4
5860
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 control of the gui that is initiating this event. So, I was thinking a trigger might be an alternate route. I just dont know how to get the username and password values that have just been updated from MS sql to Mysql.
8
2374
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 RAM with IIS 5 on Windows 2000 - 1 server Bi-xeon 3Ghz, 512 MB with MySQL 4 on Windows 2003 The website makes approximately 10.000.000 of pages seen and 310.000
0
12902
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 database system developed specifically for large enterprise databases. All advanced features of a relational database are fully implemented. - Once you purchase the product, you are only limited to the Sybase-derived engine.
2
2229
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 should only begin either letters or numbers, and Underscore character example = user123, 123user, u_ser123, user_123 = completely case insensitive
0
9691
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
9551
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,...
0
10036
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...
1
7582
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6815
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5473
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
5607
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4150
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
3
2948
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.