473,699 Members | 2,278 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 1606
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
4484
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
3523
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
3943
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
3839
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
2109
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
2037
oll3i
by: oll3i | last post by:
my librarybean package library.ejb; import java.sql.*; import javax.ejb.*; import library.common.*; @Stateless @Remote
1
2523
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
9577
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
8617
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
9174
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
9035
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...
1
8914
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7751
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...
1
6534
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
5875
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
4629
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2347
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.