Question:

How do I make these 2 MYSQL queries into one?

by  |  earlier

0 LIKES UnLike

it's tough - i can't use JOIN because there are WHERE qualifications each query needs to fit separately. I also want to order it all by the date DESC (post_date and comment_date).

$posts = mysql_query("SELECT * FROM tb1 WHERE (`feild1` LIKE $a) OR (`feild1` LIKE $b AND `feild2` LIKE 'o%' OR `feild2` LIKE 'c%' OR `feild2` LIKE 'p%') OR (`feild1` LIKE $c AND `feild2` LIKE 'o%' OR `feild2` LIKE 'c%') ORDER BY post_date DESC LIMIT 0, 5") or die(mysql_error());

$comments = mysql_query("SELECT * FROM tbl2 WHERE `feildA` LIKE '$1' ORDER BY comment_date DESC LIMIT 0, 5") or die(mysql_error());

OR maybe you can show me how to put qualifications on this query using a JOIN method:

$result = mysql_query("SELECT tbl1.*, tbl2.*, tbl3.feild1, tbl3.feild2 FROM tbl1, tbl2, tb3 ORDER BY tbl1.post_date,tbl2.comment_date,tbl3.st... DESC LIMIT 0,15") or die(mysql_error());

 Tags:

   Report

1 ANSWERS


  1. To do this you need a way to link the tables together meaning that you have to have a field that is common between the tables. So if there is a field call ID in both tbl1 and tbl2 you could do

    select * from tbl1 join tbl2 on tbl1.id = tbl2.id

Question Stats

Latest activity: earlier.
This question has 1 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.