简体   繁体   English

在基于PHP的MySQL查询中使用AND而不是JOIN

[英]Use of AND instead of JOIN with PHP based MySQL query

From what I can tell, AND cannot be used to specify two different tables, but is there another way to simply query the exact same column names in two different tables? 据我所知,AND不能用于指定两个不同的表,但是有另一种方法可以简单地在两个不同的表中查询完全相同的列名吗?

Currently I have a PHP variable: 目前我有一个PHP变量:

define('TB_RESI', 'RESI');

And a query like below. 和下面的查询一样。 I need to also query a table named RESI with the same columns...what is the best way to achieve this? 我还需要使用相同的列查询名为RESI的表...实现此目的的最佳方法是什么?

Current Query: 当前查询:

$sql = 'SELECT PictureCount AS PIC_COUNT, DATE(PictureModifiedDateTime) AS FILE_DATE, ListingRid AS HOME_ID 
            FROM ' . TB_RESI . 
            ' WHERE (COUNTY = "Lee") AND (
                ListingOfficeMLSID = "PREFP" OR 
                ListingOfficeMLSID = "PREFP2" OR 
                ListingOfficeMLSID = "PREFP3" OR 
                ListingOfficeMLSID = "PREFP4" OR 
                ListingOfficeMLSID = "PREFP6"
            ) 
            AND (PictureCount >= 4) 
            ORDER BY EntryDate DESC';

Use UNION to combine both tables 使用UNION组合两个表

$sql = 'SELECT PictureCount AS PIC_COUNT, DATE(PictureModifiedDateTime) AS FILE_DATE, ListingRid AS HOME_ID 
            FROM (SELECT * FROM ' . TB_RESI . ' UNION SELECT * FROM ' . RESI . ') AS ALL' .
            ' WHERE (COUNTY = "Lee") AND (
                ListingOfficeMLSID = "PREFP" OR 
                ListingOfficeMLSID = "PREFP2" OR 
                ListingOfficeMLSID = "PREFP3" OR 
                ListingOfficeMLSID = "PREFP4" OR 
                ListingOfficeMLSID = "PREFP6"
            ) 
            AND (PictureCount >= 4) 
            ORDER BY EntryDate DESC';

If the two tables contain a link between them use INNER JOIN to pull in the second pictures table. 如果两个表之间包含链接,请使用INNER JOIN拉入第二个图片表。

SELECT tbla.x, tbla.y, tblb.f
FROM tbla
    INNER JOIN tblb ON tblb.id = tbla.tblbid
WHERE tbla.x = 'test' AND tblb.f > 4;

I have tried to keep the example as simple as possible. 我试图让这个例子尽可能简单。

Ok, it seems like you should go down the UNION route from the comments and replies you have made. 好吧,看起来你应该从你所做的评论和回复中走下UNION路线。

SELECT PictureCount AS PIC_COUNT, DATE(PictureModifiedDateTime) AS FILE_DATE, ListingRid AS HOME_ID 
            FROM (SELECT * FROM ' . TB_RESI . ' 
            WHERE (COUNTY = "Lee") AND (
                ListingOfficeMLSID = "PREFP" OR 
                ListingOfficeMLSID = "PREFP2" OR 
                ListingOfficeMLSID = "PREFP3" OR 
                ListingOfficeMLSID = "PREFP4" OR 
                ListingOfficeMLSID = "PREFP6"
            ) 
            AND (PictureCount >= 4) 
            ORDER BY EntryDate DESC

UNION

            SELECT PictureCount AS PIC_COUNT, DATE(PictureModifiedDateTime) AS FILE_DATE, ListingRid AS HOME_ID 
            FROM (SELECT * FROM ' . TB_RESI2 . ' 
            WHERE (COUNTY = "Lee") AND (
                ListingOfficeMLSID = "PREFP" OR 
                ListingOfficeMLSID = "PREFP2" OR 
                ListingOfficeMLSID = "PREFP3" OR 
                ListingOfficeMLSID = "PREFP4" OR 
                ListingOfficeMLSID = "PREFP6"
            ) 
            AND (PictureCount >= 4) 
            ORDER BY EntryDate DESC

Where TB_RESI is the name of your first table and TB_RESI2 is the second table name 其中TB_RESI是第一个表的名称,TB_RESI2是第二个表名

If you don't want to do a JOIN for some reason, many flavors of SQL (such as MySQL) allow you to name tables, and use that name in the WHERE clause: 如果由于某种原因不想进行JOIN,可以使用多种SQL(例如MySQL)来命名表,并在WHERE子句中使用该名称:

 FROM ' . TB_RESI . ' as TBR, ' . TC_RESI . ' as TCR ' .

... ...

 WHERE TBR.field1 = TCR.field2

etc. Of course, you should be looking to see if using JOIN would be a better solution. 当然,你应该看看使用JOIN是否是一个更好的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM