我有3個表。尋找一種使用三個不同表格找到字段PRICE
的差異的好方法,然後顯示前三個最大的負面差異。我想先找到最好的MySQL查詢使用,並找到最好的方式來顯示它在PHP中。聯盟選擇MySQL的查詢,做數學和顯示在PHP
MAINTABLE:
COMPANY | MODEL | PRICE
Main Company | ProductA | 100.00
Main Company | ProductB | 50.00
Main Company | ProductC | 25.00
Main Company | ProductD | 300.00
COMPTABLE1:
COMPANY | MODEL | PRICE
Competitor1 | ProductA | 100.00 //0
Competitor1 | ProductB | 55.00 //5
Competitor1 | ProductC | 50.00 //25
Competitor1 | ProductD | 200.00 //-100
COMPTABLE2:
COMPANY | MODEL | PRICE
Competitor2 | ProductA | 99.00 //-1
Competitor2 | ProductB | 44.00 //-6
Competitor2 | ProductC | 20.00 //-5
Competitor2 | ProductD | 100.00 //-200
所以在價格上,我想顯示在我的網頁上最大的負面差異:
- Competitor2 ProductD從主要公司ProductD -200差異
- Competitor1 ProductD從主要-100差公司ProductD
- Competitor2產品B -6從主要產品B公司
IDEA區別:我不是那麼親切,但我可以在三張桌子WHERE MODEL=XXX
上使用.. UNION SELECT
。我可能會循環每一個收集數據,做數學和吐出信息。唯一的問題是,我不知道如何將每個變量存儲爲每個表的自己的價格。另外,我認爲它會顯示所有的差異,除非在數學計算之後存儲每個變量,然後顯示前三個差異。
任何想法或建議,以最好地解決這個問題將不勝感激。 (注意:不,我不能把它們全部放在一張表中= p)
美麗。到目前爲止,它在我執行的所有測試中都能正常工作,謝謝。 – ToddN 2012-04-25 17:14:03
ahem - 我的解決方案下面更靈活,更高效... – mson 2012-04-25 17:27:31
@ToddN,雖然這適用於所有產品,如果你想要一個特定的單一模型的競爭對手名單,我只是將其添加爲WHERE子句每個聯盟......或者,如果你想要每個型號的前3名,那也是一個不同的查詢。 – DRapp 2012-04-25 17:41:20