Jump to content

Recommended Posts

ok, well i had this problem in the past when selecting from 2 tables to add to a shopping cart functions.  But the solution does not seem to work in this situation.  I'm trying to SELECT from 2 tables for a SEARCH feature.  here is the sample of code im using for the query.

 

I need to SELECT from table "mczippo1" and table "partsitems".  I have tried doing a UNION, JOIN, JOIN LEFT.  I also tried making 2 seperate queries and creating a third function that = the 2 queries.  this sample below has just 1 table.  This is the code that works. anything other than this gives me an error. Please take a crack at it and add whatever to also search from table "partsitems".

 

[pre]

$strSQL = mysql_query("SELECT * FROM `mczippo1`
WHERE Detail LIKE '%$search%' 
OR Partnum LIKE '%$search%' 
OR THEME LIKE '%$search%'
OR Cat1 LIKE '%$search%' 
OR Cat2 LIKE '%$search%' 
AND OnHand > MinQty GROUP BY Style ORDER BY OnHand Desc");

$anymatches=mysql_num_rows($strSQL);

[/pre]

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/66365-select-from-2-tables/
Share on other sites

here is the union i tried... i also tried it as a join and join left, but nothing.  also tried just the 1st section as 'mczippo1,'partsitems' and it didnt work either.

 

[pre]

$strSQL = mysql_query("SELECT * FROM `mczippo1`
WHERE Detail LIKE '%$search%' 
OR Partnum LIKE '%$search%' 
OR THEME LIKE '%$search%'
OR Cat1 LIKE '%$search%' 
OR Cat2 LIKE '%$search%' 
AND OnHand > MinQty GROUP BY Style ORDER BY OnHand Desc
UNION
SELECT * FROM `partsitems`
WHERE Detail LIKE '%$search%' 
OR Partnum LIKE '%$search%' 
OR Cat1 LIKE '%$search%' 
OR Cat2 LIKE '%$search%' 
AND OnHand > MinQty ORDER BY OnHand Desc");

$anymatches=mysql_num_rows($strSQL);

[/pre]

Link to comment
https://forums.phpfreaks.com/topic/66365-select-from-2-tables/#findComment-332057
Share on other sites

they have the same fields, some are different, but the ones i need to retrieve are the same.  that was the problem I had with my other UNION for the shopping cart that I had. however this can't be the same case because they are the same exact fields that it needs to search for.... ?

Link to comment
https://forums.phpfreaks.com/topic/66365-select-from-2-tables/#findComment-332065
Share on other sites

they have the same fields, some are different, but the ones i need to retrieve are the same.  that was the problem I had with my other UNION for the shopping cart that I had. however this can't be the same case because they are the same exact fields that it needs to search for.... ?

If "some are different" then you need to specifically list the ones you want.

Link to comment
https://forums.phpfreaks.com/topic/66365-select-from-2-tables/#findComment-335195
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.