mysql选择两个表_MySQL查询从两个表中进行选择(MySQL query to select from two tables)

I am trying to write a MySQL query that uses two tables.

Table 1 contains a field named "Avail" Table 2 contains a field named "CompDate"

Both tables contains a fields named "UniqueID"

The query I need should look at table 1 where "Avail" contains "3" and table 2 "CompDate" contains, for example "2013-08-26"

If there is a match run the query.

I have tried many combinations of JOINS with no success. The aim of the query is to copy the returned data in to a new table "$NewTable" which is created prior to running this query.

My example query:

mysql_select_db($database_iPin, $conn);

$query_Hist_Temp_Insert = sprintf("INSERT INTO ".$NewTable." ( RecordID,UniqueID,HotelID,Room,AuditBy,AuditorID,LastAudit,Complete,Fail,CompleteDate)

SELECT RecordID,UniqueID,HotelID,Room,AuditBy,AuditorID,LastAudit,Complete,Fail,CompleteDate

FROM RUB_Hist

WHERE CompStamp <= '".$FromDate."'");

I have not included a JOIN in this example bacause the ones I have tried did not work.

Can anyone see how this can be done. It feels like I have been trying to get this to work for days.

Many thanks in advance for your time.

Cheers

I have used the code provided by below:

SELECT RUB_Hist.RecordID, RUB_Hist.UniqueID, CompleteDate,CompStamp

FROM RUB_Hist INNER JOIN RUB_Avail

ON RUB_Hist.UniqueID = RUB_Avail.UniqueID

WHERE RUB_Avail.Avail = '3' AND RUB_Hist.CompStamp <= '2014-01-01'

which returns zero, if I remove "AND RUB_Hist.CompStamp <= '2014-01-01'" from the query it returns data.

I have double checked to make sure that RUB_Hist.CompStamp contains record where the date is <= than my test date.

I have cut down on the fields returned for testing.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值