jojo12 Posted February 22, 2010 Share Posted February 22, 2010 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. Link to comment https://forums.phpfreaks.com/topic/192923-querying-two-tables-comparing-two-columns/ Share on other sites More sharing options...
ohdang888 Posted February 22, 2010 Share Posted February 22, 2010 to use two tables, its likethis: SELECT * FROM table1, table2 WHERE table1.column1 = '$var' AND table2.column2 = '$var' Link to comment https://forums.phpfreaks.com/topic/192923-querying-two-tables-comparing-two-columns/#findComment-1016102 Share on other sites More sharing options...
aeroswat Posted February 22, 2010 Share Posted February 22, 2010 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. Link to comment https://forums.phpfreaks.com/topic/192923-querying-two-tables-comparing-two-columns/#findComment-1016110 Share on other sites More sharing options...
jojo12 Posted February 23, 2010 Author Share Posted February 23, 2010 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 Link to comment https://forums.phpfreaks.com/topic/192923-querying-two-tables-comparing-two-columns/#findComment-1016491 Share on other sites More sharing options...
ohdang888 Posted February 23, 2010 Share Posted February 23, 2010 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); Link to comment https://forums.phpfreaks.com/topic/192923-querying-two-tables-comparing-two-columns/#findComment-1016509 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.