我需要幫助將sql查詢中的值存儲到變量中。問題是爲了得到適當的值,我寫了一個很長的UNION查詢。只有兩列,並沒有多少結果。理想情況下,我想爲每行創建一個變量,其中變量名稱是第一列的結果,該變量的相應值將是第二列的結果。將mysql行數據存儲到php變量名稱和值中
這裏是聯合查詢:
SELECT 'mon_m' as shift, count(mon_m) as count
FROM schedule
WHERE week_of = '2013-12-23'
AND mon_m <> ''
UNION
SELECT 'mon_a', COUNT(mon_a)
FROM schedule
WHERE week_of = '2013-12-23'
AND mon_a <> ''
UNION
SELECT 'mon_n', COUNT(mon_n)
FROM schedule
WHERE week_of = '2013-12-23'
AND mon_n <> ''
UNION
SELECT 'tue_m', count(tue_m)
FROM schedule
WHERE week_of = '2013-12-23'
AND tue_m <> ''
UNION
SELECT 'tue_a', COUNT(tue_a)
FROM schedule
WHERE week_of = '2013-12-23'
AND tue_a <> ''
UNION
SELECT 'tue_n', COUNT(tue_n)
FROM schedule
WHERE week_of = '2013-12-23'
AND tue_n <> ''
UNION
SELECT 'wed_m', count(wed_m)
FROM schedule
WHERE week_of = '2013-12-23'
AND wed_m <> ''
UNION
SELECT 'wed_a', COUNT(wed_a)
FROM schedule
WHERE week_of = '2013-12-23'
AND wed_a <> ''
UNION
SELECT 'wed_n', COUNT(wed_n)
FROM schedule
WHERE week_of = '2013-12-23'
AND wed_n <> ''
UNION
SELECT 'thu_m', count(thu_m)
FROM schedule
WHERE week_of = '2013-12-23'
AND thu_m <> ''
UNION
SELECT 'thu_a', COUNT(thu_a)
FROM schedule
WHERE week_of = '2013-12-23'
AND thu_a <> ''
UNION
SELECT 'thu_n', COUNT(thu_n)
FROM schedule
WHERE week_of = '2013-12-23'
AND thu_n <> ''
UNION
SELECT 'fri_m', count(fri_m)
FROM schedule
WHERE week_of = '2013-12-23'
AND fri_m <> ''
UNION
SELECT 'fri_a', COUNT(fri_a)
FROM schedule
WHERE week_of = '2013-12-23'
AND fri_a <> ''
UNION
SELECT 'fri_n', COUNT(fri_n)
FROM schedule
WHERE week_of = '2013-12-23'
AND fri_n <> ''
UNION
SELECT 'sat', count(sat)
FROM schedule
WHERE week_of = '2013-12-23'
AND sat <> ''
UNION
SELECT 'sun', COUNT(sun)
FROM schedule
WHERE week_of = '2013-12-23'
AND sun <> ''
這裏的數據想什麼小樣本:
ColumnA ColumnB
mon_m 1
mon_a 5
mon_n 6
tue_m 3
tue_a 6
tue_n 2
我需要使用PHP這樣我就可以建一個表來訪問這些值。我想使用變量訪問它們。例如:
echo $mon_m
echo $mon_a
echo $mon_n
凡
$mon_m = 1
$mon_a = 5
$mon_n = 6
我已經準備好去,需要一些幫助,讓這些值到PHP變量的SQL語句。
你可以發佈你所談論的聯合查詢? –
這是查詢條件: – PatC
您應該編輯問題並向其中添加查詢,以便人們可以投票選擇要重新打開的問題。 –