473,748 Members | 8,773 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Question regarding a MySQL SELECT statement

Hey folks!
I tried hunting for a MySQL specific NG and didn't find one but I did
see that MySQL questions get asked here. If this isn't appropriate and
you know of an appropriate NG then that would be great.

In one table I have inventory...
table name is "inventory"
columns are..
-----------------------------
item_number item_name
-----------------------------
1 radishes
2 carrots
3 potatoes
4 oranges
etc..etc

Table two is "sold"
------------------------------
date item_number
13Mar 1
14Mar 4
15Mar 2
16Mar 1
What I am querying is the table "sold" for the Unique items (1,4,2)
and then retrieving their names from table 1.

My query below isn't working..
SELECT item_name FROM inventory WHERE inventory.item_ number =
sold.item_numbe r;

I am just now starting to get into complicated MySQL queries and
actually the more I learn about it the more I love it.

I appreciate any advice anyone may have.
Many thanks!
Wilkie

Jul 17 '05 #1
8 1611
wo**********@gm ail.com wrote:
: Hey folks!
: I tried hunting for a MySQL specific NG and didn't find one but I did
: see that MySQL questions get asked here. If this isn't appropriate and
: you know of an appropriate NG then that would be great.

: In one table I have inventory...
: table name is "inventory"
: columns are..
: -----------------------------
: item_number item_name
: -----------------------------
: 1 radishes
: 2 carrots
: 3 potatoes
: 4 oranges
: etc..etc

: Table two is "sold"
: ------------------------------
: date item_number
: 13Mar 1
: 14Mar 4
: 15Mar 2
: 16Mar 1
: What I am querying is the table "sold" for the Unique items (1,4,2)
: and then retrieving their names from table 1.

: My query below isn't working..
: SELECT item_name FROM inventory WHERE inventory.item_ number =
: sold.item_numbe r;

: I am just now starting to get into complicated MySQL queries and
: actually the more I learn about it the more I love it.

: I appreciate any advice anyone may have.
: Many thanks!
: Wilkie
You need to "join" the tables.

Try google mysql join

You need to join inventory and sold on inventory.item_ number =
sold.item_numbe r

I would type out the query but I haven't used mysl recently enough to get
the syntax right in a message like this.

--

This space not for rent.
Jul 17 '05 #2
NC
wo**********@gm ail.com wrote:

In one table I have inventory... table name is "inventory"
columns are..
-----------------------------
item_number item_name
-----------------------------
1 radishes
2 carrots
3 potatoes
4 oranges
etc..etc

Table two is "sold"
------------------------------
date item_number
13Mar 1
14Mar 4
15Mar 2
16Mar 1

What I am querying is the table "sold" for the Unique items (1,4,2)
and then retrieving their names from table 1.


This should work:

SELECT DISTINCT
sold.item_numbe r AS num,
inventory.item_ name AS item
FROM inventory LEFT JOIN sold
ON inventory.item_ number = sold.item_numbe r;

Cheers,
NC

Jul 17 '05 #3
select * from inventory where item_number in (select distinct
item_number from sold);
that's it

tell me is this solved your problem or not????

Jul 17 '05 #4
Hi Vishal..
I actually got a syntax error on this one..
You have an error in your SQL syntax. Check the manual that
corresponds to your MySQL server version for the right syntax to use
near 'select distinct
item_number from sold)

By the way, I can't thank you and NC enough for the help. It's great
and appreciated.

Jul 17 '05 #5
Hi NC,
this ALMOST hits it..... I get this ..

num item
--------------------
1 radishes
4 oranges
2 carrots
NULL tangerines
NULL mushrooms
NULL potatoes
.....
.....
......
The first three are right but all the rest of the items are quoted with
NULL as the num.

Any ideas?
Thanks again..
Wilkie

Jul 17 '05 #6
NC
wo**********@gm ail.com wrote:

this ALMOST hits it..... I get this ..

num item
--------------------
1 radishes
4 oranges
2 carrots
NULL tangerines
NULL mushrooms
NULL potatoes
....
....
.....
The first three are right but all the rest of the items
are quoted with NULL as the num.


Most likely, this is because there were no tangerines,
mushrooms, or potatoes in the `sold` table. You can
correct the situation by including a WHERE clause in
your query:

SELECT DISTINCT
sold.item_numbe r AS num,
inventory.item_ name AS item
FROM inventory LEFT JOIN sold
ON inventory.item_ number = sold.item_numbe r
WHERE sold.item_numbe r IS NOT NULL;

Cheers,
NC

Jul 17 '05 #7
NC wrote:
wo**********@gm ail.com wrote:
this ALMOST hits it..... I get this ..

num item
--------------------
1 radishes
4 oranges
2 carrots
NULL tangerines
NULL mushrooms
NULL potatoes
....
....
.....
The first three are right but all the rest of the items
are quoted with NULL as the num.

Most likely, this is because there were no tangerines,
mushrooms, or potatoes in the `sold` table. You can
correct the situation by including a WHERE clause in
your query:

SELECT DISTINCT
sold.item_numbe r AS num,
inventory.item_ name AS item
FROM inventory LEFT JOIN sold
ON inventory.item_ number = sold.item_numbe r
WHERE sold.item_numbe r IS NOT NULL;

Cheers,
NC

Why not get rid of the left join? It's incorrect in this case.

SELECT DISTINCT
sold.item_numbe r AS num,
inventory.item_ name AS item
FROM
inventory,
sold
WHERE inventory.item_ number = sold.item_numbe r;

Steve
Jul 17 '05 #8
Yep, this one nailed it on the head and soundly. I like NC's
contribution above as well. I'm primarily a Perl programmer and I've
always liked the philosophy of TMTOWTDI. MySQL appears to be similar.

Wilkie

Jul 17 '05 #9

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

Similar topics

8
4491
by: Tony Clarke | last post by:
Hi, Just a quick question about performance with MySQL & PHP. If I had a table in a MySQL database with about 100,000 records in it and I need to find the last record is there a quick way to do this, other than going through the whole database to find the last record? Would the amount of records in the table cause major performance problems? For example, if I had a html form that people used to log something and needed to display a...
0
3527
by: Lenz Grimmer | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi, MySQL 4.0.14, a new version of the popular Open Source/Free Software Database, has been released. It is now available in source and binary form for a number of platforms from our download pages at http://www.mysql.com/downloads/ and mirror sites.
0
3946
by: Mike Chirico | last post by:
Interesting Things to Know about MySQL Mike Chirico (mchirico@users.sourceforge.net) Copyright (GPU Free Documentation License) 2004 Last Updated: Mon Jun 7 10:37:28 EDT 2004 The latest version of this document can be found at: http://prdownloads.sourceforge.net/souptonuts/README_mysql.txt?download
5
3844
by: jayson_13 | last post by:
Hi, I need to implement a counter and i face problem of locking so hope that u guys can help me. I try to do test like this : 1st connection SELECT * FROM nextkey WHERE tblname = 'PLCN' FOR Update; (when i execute this statement and i guess that this will lock the
5
2112
by: Jeremy | last post by:
I am relatively inexperienced with SQL, and I am trying to learn how to analyze some data with it. I have a table with the following information. COMPANY ID , DATE, MarektValue I would like to select the largest 100 marketvalues for each date in my table. How can I do this efficiently in MySql.
5
2042
oll3i
by: oll3i | last post by:
my librarybean package library.ejb; import java.sql.*; import javax.ejb.*; import library.common.*; @Stateless @Remote
1
2526
by: paulq182 | last post by:
PLEASE HELP ME WITH MY CODE?? import java.sql.*; import java.io.*; class min_filmdb_rel_mysql { public static void main (String args ) throws SQLException, IOException {
1
9582
ssnaik84
by: ssnaik84 | last post by:
Hi Guys, Last year I got a chance to work with R&D team, which was working on DB scripts conversion.. Though there is migration tool available, it converts only tables and constraints.. Rest of things (stored procedures, functions).. we have to manually edit. That time, we face some interesting challenges.. I failed to document all of them, but whatever I can share with u.. I will try.. :) ...
0
8991
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
8831
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
9548
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9374
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8244
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
6076
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
4607
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
4876
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3315
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

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.