473,396 Members | 2,082 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.

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_number;

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 1591
wo**********@gmail.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_number;

: 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_number

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**********@gmail.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_number AS num,
inventory.item_name AS item
FROM inventory LEFT JOIN sold
ON inventory.item_number = sold.item_number;

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**********@gmail.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_number AS num,
inventory.item_name AS item
FROM inventory LEFT JOIN sold
ON inventory.item_number = sold.item_number
WHERE sold.item_number IS NOT NULL;

Cheers,
NC

Jul 17 '05 #7
NC wrote:
wo**********@gmail.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_number AS num,
inventory.item_name AS item
FROM inventory LEFT JOIN sold
ON inventory.item_number = sold.item_number
WHERE sold.item_number IS NOT NULL;

Cheers,
NC

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

SELECT DISTINCT
sold.item_number AS num,
inventory.item_name AS item
FROM
inventory,
sold
WHERE inventory.item_number = sold.item_number;

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
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...
0
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...
0
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...
5
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'...
5
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...
5
oll3i
by: oll3i | last post by:
my librarybean package library.ejb; import java.sql.*; import javax.ejb.*; import library.common.*; @Stateless @Remote
1
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
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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
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
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,...
0
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...
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
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,...

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.