473,386 Members | 1,712 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,386 software developers and data experts.

Combining 2 MySQL tables in 1 query

258 100+
Hi everyone
I have 2 DB tables which I want to combine in 1 query.


stores :
Expand|Select|Wrap|Line Numbers
  1. +-id-+-Stores name-+
  2. |    |             |
  3. | 1  |  first      |
  4. | 2  |  second     |
  5. | 3  |  third      |
  6. +----+-------------+
  7.  
Products :
Expand|Select|Wrap|Line Numbers
  1. +-id+-storesid-+-productname-+
  2. |   |          |             |
  3. | 1 |   3      |  pen        |
  4. | 2 |   2      |  DVD        |
  5. | 3 |   3      |  Hard drive |
  6. | 4 |   1      |  DVD        |
  7. | 5 |   2      |  mp3 player |
  8. | 6 |   2      |  pen        |
  9. | 7 |   1      |  Hard drive |
  10. +---+----------+-------------+
  11.  

I'm sure this is possible but I don't know how

as you can see the first table contains the name and ID of each store. and the second one contains products of each store.

Here's what I need to do :

When a user searches for a product (Ex. pen) the query should return all of the stores that have that product.

Right now i'm doing this by 2 queries. First I get the ID of those stores like this:
Expand|Select|Wrap|Line Numbers
  1. SELECT storesid FROM products WHERE productname='pen'
  2.  
Then I fetch all of the IDs and put them in an array and then I execute the second query like this :
Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM stores WHERE id=3 OR id=2
  2.  
As you know this query could get very long depending on the number of stores which have that product

So the question is if I can do this job in one query.



Thanks / Behzad
Nov 14 '10 #1

✓ answered by yarbrough40

Expand|Select|Wrap|Line Numbers
  1. SELECT * 
  2. FROM stores JOIN products on stores.id = products.storesid
  3. WHERE products.productname = 'pen'
  4.  
Enjoy!

2 1490
yarbrough40
320 100+
Expand|Select|Wrap|Line Numbers
  1. SELECT * 
  2. FROM stores JOIN products on stores.id = products.storesid
  3. WHERE products.productname = 'pen'
  4.  
Enjoy!
Nov 15 '10 #2
bnashenas1984
258 100+
Thanks yarbrough40
I appreciate your help
Nov 16 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Benjamin Dickgießer | last post by:
Hi, I want to create a sql query but don't know if this is possible with mysql. The Query should do the following: Select all db entries from table in which entry a is smaller than the number...
5
by: Perttu Pulkkinen | last post by:
I want to copy certain tables from server to server b. How do I make it fastly and efficiently with PHP? I wrote below my current start, but it may not be the best approach. Perttu Pulkkinen,...
6
by: Xenophobe | last post by:
I know this isn't a MySQL forum, but my question is related to a PHP project. I have two tables. table1 table2 "table1" contains 2 columns, ID and FirstName:
0
by: jerome iffrig | last post by:
Hi, I am willing to setup my MySQL tables so they can hold international strings (English, French, German and Spanish). I understand that Unicode character coding is a good candidate to do so....
0
by: buster100 | last post by:
I am looking for an example of php code that accesses a mysql table using SSL (php v4.3.10). I have php code on serverA and some mysql tables on serverB (located on the other side of the internet...
2
by: fusonts | last post by:
Can someon show me how to wirte a mySQL search query to search from only the open records. This is what I have but it continues to return records that are closed so I'm obviously doing something...
0
by: data monkey | last post by:
My company's web application uses mySQL databases. I have linked these tables (Read only) into Access 2007 so I can create queries and reports. Now I need to extract some of the content from these...
1
by: banging | last post by:
Hi there, I have a question regarding locking of tables so that when two or more people try to write or update the mysql tables, it locks up. Basically I only want one person to write to the...
3
by: xoinki | last post by:
hi all, I wanted to know few general details about MySQL slow query log. My slow log has a statement like # User@Host: root @ localhost here, what does this "" signify? why is this needed?...
2
by: secutos | last post by:
I have the option of storing data online as text files, or storing data online in a MySQL database. If the information is stored as a text file, all I have to do is a HttpWebRequest to the text file...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
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
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...

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.