MySQL查询打印双记录

时间:2013-11-11 17:27:00

标签: php mysql double

sales&中的所有录音机purchase表仅输入一次。我仔细检查了一下。但是,我希望以这样的方式组合这两个表,以便完全获取这两个表。这是我的查询 请注意timestamp列具有mysql DATE格式

Select 
    sales.ID as sid,
    sales.saleHatsh,
    sales.timestamp as sdate,
    sales.gatePass as sGP,
    sales.pname as sPN,
    sales.description as sDES,
    sales.balance as sbal, 
    purchase.ID as pid,
    purchase.purchaseHatsh,
    purchase.timestamp as pdate,
    purchase.gatePass as pGP,
    purchase.pname as pPN,
    purchase.description as pDES,
    purchase.balance as pbal 
from sales,purchase 
where sales.timestamp='2013-11-11' OR purchase.timestamp='2013-11-11'

这是我的查询结果& sales& purchaseQuery Result Sales表只有2个记录器 enter image description here

购买表只有4个记录器 enter image description here

1 个答案:

答案 0 :(得分:2)

发生的事情是你没有以任何方式加入这些表格。所以你从这些表中得到了所有可能的匹配。查看列,我认为您不想加入,但可能需要UNION代替:

SELECT 
  sales.ID AS id,
  sales.saleHatsh AS hatsch,
  sales.TIMESTAMP AS date,
  sales.gatePass AS GatePass,
  sales.pname AS pname,
  sales.description AS Description,
  sales.balance AS balance,
  'SALE' AS transanctionType
FROM sales
WHERE sales.TIMESTAMP = '2013-11-11'
UNION 
SELECT 
  purchase.ID,
  purchase.purchaseHatsh,
  purchase.TIMESTAMP,
  purchase.gatePass,
  purchase.pname,
  purchase.description,
  purchase.balance,
  'PURCHASE'
FROM purchase
WHERE purchase.TIMESTAMP = '2013-11-11'

我添加了一个“transactionType”列,供您识别哪些是销售或购买。