I have two tables:
table1:
id email
1 john#gmail.com
2 doe#gmail.com
table2:
userid key value
1 firstname john
1 phone 112233
2 firstname doe
2 phone 223344
This is mysql query without condition:
SELECT a.*,
b.*,
GROUP_CONCAT(b.key),
GROUP_CONCAT(b.value)
FROM table1 a
LEFT JOIN table2 b ON a.id = b.userid
This is result:
array(
[id] => 1
[email] => john#gmail.com
[userid] => 1
[key] => firstname,phone
[value] => john,112233
)
array(
[id] => 2
[email] => doe#gmail.com
[userid] => 2
[key] => firstname,phone
[value] => doe,223344
)
This is mysql query with condition:
SELECT a.*,
b.*,
GROUP_CONCAT(b.key),
GROUP_CONCAT(b.value)
FROM table1 a
LEFT JOIN table2 b ON a.id = b.userid
WHERE b.key = "firstname"
AND b.value LIKE '%jo%'
And this is result:
array(
[id] => 1
[email] => john#gmail.com
[userid] => 1
[key] => firstname
[value] => john
)
But I want this:
array(
[id] => 1
[email] => john#gmail.com
[userid] => 1
[key] => firstname,phone
[value] => john,112233
)
There any way to do this? thank for any help!
Your queries are lacking the GROUP BY clause to get a row per user. Then use a HAVING clause to make sure the aggregated row includes a firstname '%jo%':
SELECT a.*,
GROUP_CONCAT(b.key),
GROUP_CONCAT(b.value)
FROM table1 a
LEFT JOIN table2 b ON a.id = b.userid
GROUP BY a.id
HAVING sum(b.key = 'firstname'
AND b.value LIKE '%jo%') > 0;
true results in 1, false in 0 in MySQL. So checking whether the sum is greater than zero means checking whether the condition is true at least once.
Can help me, I have something problem about query select distinct, :
My table
table_pemesan
id_pms
table_pesan
no_psn
id_pms
table_dpesan
no_psn
My query mysql
select pm.id_pms,
pm.nama,
pm.date_create,
ps.no_psn,
ps.status,
dps.no_dpsn
FROM pemesan as pm
INNER JOIN pesan ps on ps.id_pms=pm.id_pms
INNER JOIN dpesan dps on dps.no_psn=ps.no_psn
ORDER BY pm.id_pms ASC
//output
Array
(
[0] => Array
(
[id_pms] => 1
[nama] => Isnan
[date_create] => 2014-05-28 23:54:54
[no_psn] => 1
[status] => sedang diproses
[no_dpsn] => 1
)
[1] => Array
(
[id_pms] => 1
[nama] => Isnan
[date_create] => 2014-05-28 23:54:54
[no_psn] => 1
[status] => sedang diproses
[no_dpsn] => 2
)
)
the array index 0 & 1 the same data, and my question how to distinct the query? thanks b4, sorry my english not good..
Try to use this query:-
SELECT DISTINCT pm.id_pms, pm.nama, pm.date_create, ps.no_psn, ps.status, dps.no_dpsn
FROM pemesan as pm
INNER JOIN pesan ps on ps.id_pms=pm.id_pms
INNER JOIN dpesan dps on dps.no_psn=ps.no_psn
ORDER BY pm.id_pms ASC
I'm in a bind with a deadline and I cannot seem to figure this out.
I am trying to query the table to get the values for the corresponding max shipdate. My query is below. This is for Fox Pro using an ODBC driver.
SELECT
so1.sono,
so1.custno,
so1.item,
so1.shipdate as last_shipdate,
so1.price as last_price
FROM sotran01 so1
INNER JOIN (
SELECT
custno,
item,
MAX(shipdate) as last_shipdate
FROM sotran01
WHERE shipdate >= {d'2013-05-23'}
AND shipdate <= {d'2014-05-23'}
GROUP BY custno, item
) so2 ON (so1.custno = so2.custno AND so1.item = so2.item AND so1.shipdate = so2.last_shipdate)
WHERE so1.item IN (
SELECT item
FROM arpric01
)
ORDER BY so1.custno, so1.item, so1.shipdate
This is what I get (using ADOdb):
ADODB_vfp Object
(
[databaseType] => vfp
[fmtDate] => {^Y-m-d}
[fmtTimeStamp] => {^Y-m-d, h:i:sA}
[replaceQuote] => '+chr(39)+'
[true] => .T.
[false] => .F.
[hasTop] => top
[_bindInputArray] =>
[sysTimeStamp] => datetime()
[sysDate] => date()
[ansiOuter] => 1
[hasTransactions] =>
[curmode] =>
[dataProvider] => odbc
[hasAffectedRows] => 1
[binmode] => 1
[useFetchArray] =>
[_genSeqSQL] => create table %s (id integer)
[_autocommit] => 1
[_haserrorfunctions] => 1
[_has_stupid_odbc_fetch_api_change] => 1
[_lastAffectedRows] => 0
[uCaseTables] => 1
[_dropSeqSQL] => drop table %s
[database] =>
[host] => DRIVER={Microsoft Visual FoxPro Driver};SOURCETYPE=dbf;SOURCEDB=C:\Sites\hub.fieldfresh.dev\_cache\VP10\PRAXIS\;EXCLUSIVE=NO;
[user] =>
[password] =>
[debug] =>
[maxblobsize] => 262144
[concat_operator] => +
[substr] => substr
[length] => length
[random] => rand()
[upperCase] => upper
[nameQuote] => "
[charSet] =>
[metaDatabasesSQL] =>
[metaTablesSQL] =>
[uniqueOrderBy] =>
[emptyDate] =>
[emptyTimeStamp] =>
[lastInsID] =>
[hasInsertID] =>
[hasLimit] =>
[readOnly] =>
[hasMoveFirst] =>
[hasGenID] =>
[genID] => 0
[raiseErrorFn] =>
[isoDates] =>
[cacheSecs] => 3600
[memCache] =>
[memCacheHost] =>
[memCachePort] => 11211
[memCacheCompress] =>
[sysUTimeStamp] =>
[arrayClass] => ADORecordSet_array
[noNullStrings] =>
[numCacheHits] => 0
[numCacheMisses] => 0
[pageExecuteCountRows] => 1
[uniqueSort] =>
[leftOuter] =>
[rightOuter] =>
[autoRollback] =>
[poorAffectedRows] =>
[fnExecute] =>
[fnCacheExecute] =>
[blobEncodeType] =>
[rsPrefix] => ADORecordSet_
[autoCommit] => 1
[transOff] => 0
[transCnt] => 0
[fetchMode] => 2
[null2null] => null
[bulkBind] =>
[_oldRaiseFn] =>
[_transOK] =>
[_connectionID] => Resource id #8
[_errorMsg] => [Microsoft][ODBC Visual FoxPro Driver]Syntax error.
[_errorCode] => 37000
[_queryID] =>
[_isPersistentConnection] =>
[_evalAll] =>
[_affected] =>
[_logsql] =>
[_transmode] =>
[_error] =>
)
The error doesn't say much. I can copy and paste the code into MySQL and it runs fine and returns what I expect. Hopefully another set of eyes, with more Fox Pro experience, can see what the issue is here.
Thanks for any assistance.
Your subquery is incorrect. This is the subquery:
INNER JOIN (
SELECT custno, item, MAX(shipdate) as last_shipdate
FROM sotran01
WHERE shipdate >= {d'2013-05-23'}
AND shipdate <= {d'2014-05-23'}
GROUP BY custno, item, last_shipdate
-------------------------------^
)
That is an aggregation column. Remove it:
INNER JOIN (
SELECT custno, item, MAX(shipdate) as last_shipdate
FROM sotran01
WHERE shipdate >= {d'2013-05-23'}
AND shipdate <= {d'2014-05-23'}
GROUP BY custno, item
)
I am trying to output a concated value from my query but am having some issues.
Here is my query:
$result = mysql_query(
"SELECT c.company, n.nid, n.createdOn, CONCAT_WS(' ',c2.fname,c2.lname), CONCAT_WS(' ',c3.fname,c3.lname), n.urgent, n.description
FROM notes n
INNER JOIN Positions p ON FIND_IN_SET(p.id, n.forDepts) > 0
LEFT JOIN companies c ON c.userid = n.clientId
LEFT JOIN companies c2 ON c2.userid = n.createdBy
LEFT JOIN companies c3 ON c3.userid = n.claimedBy
GROUP BY n.nid
LIMIT 0,100"
);
My array is printing like so:
Array ( [0] => Honda of Kirkland [company] => Honda of Kirkland [1] => 1 [nid] => 1 [2] => 2009-09-28 21:33:15 [createdOn] => 2009-09-28 21:33:15 [3] => [CONCAT_WS(' ',c2.fname,c2.lname)] => [4] => [CONCAT_WS(' ',c3.fname,c3.lname)] => [5] => 0 [urgent] => 0 [6] => Milestones [description] => Milestones )
I am trying it like this, but it does not work:
while ($row = mysql_fetch_array($result)) {
$created_by = $row[3];
}
so I'm working on a tool for fantasy baseball. I constructed this SQL statement that I thought worked initially but turned out to not do so after I added more rows.
Here is the statement:
SELECT
p.player_id, position, last_name, first_name, player_type, team, s.season_gp, s.season_runs, s.season_hits, s.season_doubles, s.season_triples, s.season_home_runs, s.season_runs_batted_in, s.season_base_on_balls, s.season_strikeouts, s.season_stolen_bases, w.week_gp, w.week_runs, w.week_hits, w.week_doubles, w.week_triples, w.week_home_runs, w.week_runs_batted_in, w.week_base_on_balls, w.week_strikeouts, w.week_stolen_bases
FROM
player AS p
LEFT JOIN
(SELECT
player_id, COUNT(date) AS week_gp, SUM(runs) AS week_runs, SUM(hits) AS week_hits, SUM(doubles) AS week_doubles, SUM(triples) AS week_triples, SUM(home_runs) AS week_home_runs, SUM(runs_batted_in) AS week_runs_batted_in, SUM(base_on_balls) AS week_base_on_balls, SUM(strikeouts) AS week_strikeouts, SUM(stolen_bases) AS week_stolen_bases
FROM
game_mlb
WHERE
date >= CURDATE() - INTERVAL 1 WEEK) w
ON p.player_id = w.player_id
LEFT JOIN
(SELECT
player_id, COUNT(date) AS season_gp, SUM(runs) AS season_runs, SUM(hits) AS season_hits, SUM(doubles) AS season_doubles, SUM(triples) AS season_triples, SUM(home_runs) AS season_home_runs, SUM(runs_batted_in) AS season_runs_batted_in, SUM(base_on_balls) AS season_base_on_balls, SUM(strikeouts) AS season_strikeouts, SUM(stolen_bases) AS season_stolen_bases
FROM
game_mlb
WHERE
date >= YEAR(CURDATE())) s
ON p.player_id = s.player_id
WHERE
team_id = 1
Here are the PHP arrays for the tables being used.
game_mlb:
$game_mlb = array(
array('game_id' => '9','player_id' => '1','date' => '2013-03-31','at_bats' => '4','runs' => '0','hits' => '0','doubles' => '0','triples' => '0','home_runs' => '0','runs_batted_in' => '0','stolen_bases' => '0','base_on_balls' => '0','strikeouts' => '0','innings_pitched' => NULL,'wins' => NULL,'complete_games' => NULL,'shutouts' => NULL,'saves' => NULL,'earned_runs' => NULL,'strikeouts_pitched' => NULL,'holds' => NULL),
array('game_id' => '10','player_id' => '2','date' => '2013-04-01','at_bats' => '4','runs' => '0','hits' => '0','doubles' => '0','triples' => '0','home_runs' => '0','runs_batted_in' => '0','stolen_bases' => '0','base_on_balls' => '1','strikeouts' => '1','innings_pitched' => NULL,'wins' => NULL,'complete_games' => NULL,'shutouts' => NULL,'saves' => NULL,'earned_runs' => NULL,'strikeouts_pitched' => NULL,'holds' => NULL)
);
player:
$player = array(
array('player_id' => '1','player_league' => 'MLB','first_name' => 'Adrian','last_name' => 'Beltre','player_type' => 'MLB_BATTER','position' => '3B','team' => 'TEX','yahoo_id' => '6039','minors_type' => 'MLB_BR','minors_id' => 'beltre001adr','injury' => NULL,'team_id' => '1'),
array('player_id' => '2','player_league' => 'MLB','first_name' => 'Albert','last_name' => 'Pujols','player_type' => 'MLB_BATTER','position' => '1B','team' => 'LAA','yahoo_id' => '6619','minors_type' => 'MLB_BR','minors_id' => 'pujols001jos','injury' => NULL,'team_id' => '1')
);
And here is the result I'm getting:
$player = array(
array('player_id' => '1','position' => '3B','last_name' => 'Beltre','first_name' => 'Adrian','player_type' => 'MLB_BATTER','team' => 'TEX','season_gp' => '2','season_runs' => '0','season_hits' => '0','season_doubles' => '0','season_triples' => '0','season_home_runs' => '0','season_runs_batted_in' => '0','season_base_on_balls' => '1','season_strikeouts' => '1','season_stolen_bases' => '0','week_gp' => '2','week_runs' => '0','week_hits' => '0','week_doubles' => '0','week_triples' => '0','week_home_runs' => '0','week_runs_batted_in' => '0','week_base_on_balls' => '1','week_strikeouts' => '1','week_stolen_bases' => '0'),
array('player_id' => '2','position' => '1B','last_name' => 'Pujols','first_name' => 'Albert','player_type' => 'MLB_BATTER','team' => 'LAA','season_gp' => NULL,'season_runs' => NULL,'season_hits' => NULL,'season_doubles' => NULL,'season_triples' => NULL,'season_home_runs' => NULL,'season_runs_batted_in' => NULL,'season_base_on_balls' => NULL,'season_strikeouts' => NULL,'season_stolen_bases' => NULL,'week_gp' => NULL,'week_runs' => NULL,'week_hits' => NULL,'week_doubles' => NULL,'week_triples' => NULL,'week_home_runs' => NULL,'week_runs_batted_in' => NULL,'week_base_on_balls' => NULL,'week_strikeouts' => NULL,'week_stolen_bases' => NULL)
);
This is incorrect. Beltre and Pujols should each have 1 game played with their respective stats, but Beltre is getting credited with everything.
Any help would be appreciated, I've been stuck on this for a while now.
You need group by statements in your subquery:
SELECT p.player_id, position, last_name, first_name, player_type, team,
s.season_gp, s.season_runs, s.season_hits, s.season_doubles, s.season_triples, s.season_home_runs, s.season_runs_batted_in, s.season_base_on_balls, s.season_strikeouts, s.season_stolen_bases, w.week_gp, w.week_runs, w.week_hits, w.week_doubles, w.week_triples, w.week_home_runs, w.week_runs_batted_in, w.week_base_on_balls, w.week_strikeouts, w.week_stolen_bases
FROM player p LEFT JOIN
(SELECT player_id, COUNT(date) AS week_gp, SUM(runs) AS week_runs, SUM(hits) AS week_hits, SUM(doubles) AS week_doubles, SUM(triples) AS week_triples, SUM(home_runs) AS week_home_runs, SUM(runs_batted_in) AS week_runs_batted_in, SUM(base_on_balls) AS week_base_on_balls, SUM(strikeouts) AS week_strikeouts, SUM(stolen_bases) AS week_stolen_bases
FROM game_mlb
WHERE date >= CURDATE() - INTERVAL 1 WEEK
group by player_id
) w
ON p.player_id = w.player_id LEFT JOIN
(SELECT player_id, COUNT(date) AS season_gp, SUM(runs) AS season_runs, SUM(hits) AS season_hits, SUM(doubles) AS season_doubles, SUM(triples) AS season_triples, SUM(home_runs) AS season_home_runs, SUM(runs_batted_in) AS season_runs_batted_in, SUM(base_on_balls) AS season_base_on_balls, SUM(strikeouts) AS season_strikeouts, SUM(stolen_bases) AS season_stolen_bases
FROM game_mlb
WHERE date >= YEAR(CURDATE())
group by player_id
) s
ON p.player_id = s.player_id
WHERE team_id = 1;
In most versions of SQL, your code would have returned an error. However, MySQL allows columns in the select list that are not in the group by -- and this has caused your problem.