Jump to content

[SOLVED] Query question


LOUDMOUTH

Recommended Posts

I am trying to show info from more than one Database Table

 

The  'items1' is working but I would like to know how to also display 'items2' 'items3' 'items4' tables in the same area. 

 

mysql_query("SELECT * FROM `items1` WHERE `id`='".$line['itemid']."'");

 

Thanks for any help

Link to comment
Share on other sites

You will need to look into the JOIN functionality of SQL.

 

Not knowing anything about your schema, its hard to help, but maybe something like this is what you are looking for:

 

SELECT * 
FROM `items1`
JOIN `items2` ON `items2`.`id` = `items1`.`id`
JOIN `items3` ON `items3`.`id` = `items1`.`id`
JOIN `items4` ON `items4`.`id` = `items1`.`id`
WHERE `items1`.`id`='".$line['itemid']."'"

Link to comment
Share on other sites

I want to show 'items1' 'items2' 'items3' and all the way upto 'items40'

 

<?
$result = mysql_query("SELECT * FROM `inventory` WHERE `userid` = '".$user_class->id."' ORDER BY `itemid` ASC");
$howmanyitems = 0;
while($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
$result2 = mysql_query("SELECT * FROM `items1` WHERE `id`='".$line['itemid']."'");
    $worked2 = mysql_fetch_array($result2);

Link to comment
Share on other sites

Why do You have 40 tables for stores?

 

Make one table and name it store, another table name it items (items in stock) and make a table between to tell which store has which items. At least that sound's more logical to me.

 

Joining 40 tables for a report really sounds too much.

Link to comment
Share on other sites

No you can not join 40 seperate tables that have no relations.  You can Union them, but it's just a really bad design and I can't recommend it. 

 

With that said, the answer is:  UNION.

 

SELECT * FROM item1
UNION
SELECT * FROM item2
UNION

 

etc.

 

The structure of all the item tables needs to be the same.

Link to comment
Share on other sites

Well You can join them all if You have to join them by something, but as it sounds I think You need UNION of all tables.

Try something like:

 

( SELECT col1, col2, col3 FROM item1 WHERE conditions )

UNION

( SELECT col1, col2, col3 FROM item2 WHERE conditions )

UNION

( SELECT col1, col2, col3 FROM item3 WHERE conditions )

.

.

.

( SELECT col1, col2, col3 FROM item40 WHERE conditions )

 

Have in mind that the number of columns selected should be the same in all selects.

Try reading this for more info.

Link to comment
Share on other sites

Best way would be to Make just one ITEMS table and then associate the items to the correct Store Locations? 

 

Thanks for the help guys

 

We don't know about your application, but on a basic level, yes.  You have a Store table, with a key (store_id).  One row per store.

 

You might also need an item table from the sound of it. (Key: item_id).  One row per item.

 

Then you have storeItem table.  If for example this track inventory, you'd have something like:

 

 

storeItem

------------

storeItem_id  pk

item_id ---> Identifies the item

store_id ---> Identifies the store

qty  ---> quantity on hand.

 

 

Just a guess given the small amount of info we have from you.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.