A
A
Art. Ku.2015-07-30 11:04:38
MySQL
Art. Ku., 2015-07-30 11:04:38

How to assign a separate WHERE for each when querying two tables?

Hello. I'm trying to combine two database queries.
The task is to output from comments when comments.post LIKE '$docid' , and from dell when dell.adm LIKE '$adm'.

$sql = " SELECT  dell.ban, dell.adm, comments.post, comments.id, comments.parent_id, comments.name, comments.comment, comments.head
FROM dell, comments WHERE dell.adm LIKE '$adm' /* как их написать вместе? */ comments.post LIKE '$docid' ORDER BY comments.id DESC	";

Help. I didn't find any examples on the web. Is it even possible? Thank you.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
svd71, 2015-07-30
@kukaew

If I understood correctly, then you need to issue some of the records from the comments table with your condition and add records from dell with your condition to the received records. At the same time, both tables do not have common keys to each other and some kind of common table ( !? ). Without such a condition, you get a cortesian: each entry in their comments will be repeated as many times as there are entries in dell.
If you need all the same such separate pieces from these tables, then you can use the union. But the trick is that missing columns from two tables need to be simulated:

Select
comments.post, comments.id, comments.parent_id, comments.name, 
Cast(0 as integer) as ban, cast('' as varchar(200) as adm
from comments where comments.post LIKE '$docid'

UNION

select cast('' as varchar(250)), 0 , 0, '', ban, adm from dell where dell.adm LIKE '$adm'

D
Dmitry Kovalsky, 2015-07-30
@dmitryKovalskiy

I advise you not to write a query in 2 tables separated by commas. At the same time, xs what Join is done and readability drops to zero. I recommend rewriting with an explicit Join with an explicit indication of which fields and conditions. And inside Where already write all the conditions you need

S
Sergey Ivanov, 2015-07-30
@Writerim

SELECT * FROM `test`,`test2` WHERE `test`.`id` = 15 AND `test2`.`id` = 150

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question