NogDog;10940971 wrote:I would suggest specifying the exact columns you need returned in the SELECT. If there is any possible ambiguity as to which table each is from, use the table name (or alias) as a prefix, plus you might want to provide an alias:
SELECT table1.foo AS foo, table2.bar AS bar
FROM table1
INNER JOIN table2 ON table2.t1_id = table1.id
. . .
Then you can be assured that the two columns wanted in this example will be indexed as 'foo' and 'bar' in the fetched array for reach result row, plus each result will contain only the necessary data and not any columns that will not be used by the application code.
Thanks for the reply,
Just need a bit of clarification.
The table in the database consists of 13 columns and five of those columns are foreign keys referencing the primary key in their related table. These are the columns with the last 5 being the foreign keys:
itemID,
itemTitle,
itemSKULadies,
itemSKUMen,
itemDescLadies,
itemDescMen,
itemPrice,
itemColours,
sizeLadiesID,
sizeMenID,
catID,
supplierID,
itemTypeID';
So since there are 5 foreign tables that need to be dealt with I'm not sure how the query would go or whether it would be best to use a left join instead of an inner join as you suggested. I came up with the following which doesn't include aliases at this stage as I just want to get a working code block before I tweak it with aliases which are advanced for me:
$select = 'SELECT itemID, itemTitle, itemSKULadies, itemSKUMen, itemDescLadies, itemDescMen, itemPrice, itemColours, categories.category, suppliers.supplier, itemTypes.itemType, sizesMen.size, sizesLadies.size';
$from = ' FROM items LEFT JOIN categories, suppliers, itemTypes, sizesMen, sizesLadies';
$where = ' WHERE categories.catID=items.catID, suppliers.supplierID=items.supplierID, itemTypes.itemTypeID=items.itemTypeID, sizesLadies.sizeLadiesID=items.sizeLadiesID, sizesMen.sizeMenID=items.sizeMenID';
It gave me the following error though:
Error fetching items: 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 ' suppliers, itemTypes, sizesMen, sizesLadies WHERE categories.catID=items.catID,' at line 1
Can you comment further on this SQL code as I'm not sure what the error means.
Appreciate any further advice.