Jump to content

querying two tables, comparing two columns


jojo12

Recommended Posts

Having problems with my mySQL / PHP code if anyone can help me :)

 

Trying to search on two tables in my DB, both with identical columns, currently I can search and display the results fine from one table:

 

$SQL = "SELECT * FROM table WHERE (column1 LIKE '%".$_GET["search"]."%' or column2 LIKE '%".$_GET["search"]."%')";

 

But I'm lost on what I need to do to get this to run across the two tables, and then another SQL command once I have the above to compare the two columns where data is matched on the above.

Having problems with my mySQL / PHP code if anyone can help me :)

 

Trying to search on two tables in my DB, both with identical columns, currently I can search and display the results fine from one table:

 

$SQL = "SELECT * FROM table WHERE (column1 LIKE '%".$_GET["search"]."%' or column2 LIKE '%".$_GET["search"]."%')";

 

But I'm lost on what I need to do to get this to run across the two tables, and then another SQL command once I have the above to compare the two columns where data is matched on the above.

 

To add on to the other guys comment look into the AS keyword if you don't want to keep writing table1.columnname and table2.columnname over and over again.

I've done something like:

 

$query = "SELECT * FROM table1,table2 WHERE table1.column LIKE \"%$var%\" OR table2.column LIKE  \"%$var%\" 

 

However I get a mysql_num_rows(): supplied argument is not a valid query error - If I change the query to do only one table, with the same columns from that table, the query works fine however :(

well, i may sound stupid here, but  just to cover the basics: you did run the query, right?

 

like this:

$query = "SELECT * FROM table1,table2 WHERE table1.column LIKE \"%$var%\" OR table2.column LIKE  \"%$var%\" ;
$result = mysql_query($query) or die(mysql_error());
echo mysql_num_rows($result);

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.