Display ALL rows from query

ellucidone :

My script is showing me one row, even though it should be showing 86 of them based on my results. Only 1 ID has notes associated with it. What am I doing wrong with this query?

'SELECT x.*, GROUP_CONCAT(y.note ORDER BY y.note_id separator " ") as note
 from
 (
     SELECT A.* from vzw_missopportunity A
       INNER JOIN locations l ON A.Agent_ID = l.Outlet_ID
       WHERE A.Agent_ID = "'.$_SESSION['agent'].'"
       AND l.district = "'.$_SESSION['district'].'"
 ) as x
 inner join
 (

  SELECT note, note_id from notes

 ) as y on x.ID = y.note_id
 ';

From Print_r of query

SELECT x.*, GROUP_CONCAT(y.note ORDER BY y.note_id separator " ") as note from ( SELECT A.* from vzw_missopportunity A INNER JOIN locations l ON A.Agent_ID = l.Outlet_ID WHERE A.Agent_ID = "130400" AND l.district = "District 1" ) as x inner join ( SELECT note, note_id from notes ) as y on x.ID = y.note_id

SQL Dump

CREATE TABLE `notes` (
  `ID` int(10) NOT NULL,
  `note_id` varchar(10) NOT NULL,
  `note` varchar(500) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;


INSERT INTO `notes` (`ID`, `note_id`, `note`) VALUES
(3, '47', 'asdfasdf'),
(4, '638', 'asdfasdfasdf'),
(5, '47', 'This is a test of Notes'),
(6, '47', 'This is another Note'),
(7, '47', 'Antoher Note');


ALTER TABLE `notes`
  ADD PRIMARY KEY (`ID`);

ALTER TABLE `notes`
  MODIFY `ID` int(10) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=8;
COMMIT;


The results should show all rows for the outlet ID and match the notes for that row if they exist. Currently it is only showing the result that has notes associated with it.

missed opp table

-- phpMyAdmin SQL Dump
-- version 4.9.1
-- https://www.phpmyadmin.net/
-- Generation Time: Feb 25, 2020 at 10:54 AM
-- Server version: 10.1.41-MariaDB-0+deb9u1
-- PHP Version: 7.3.10-1+0~20191008.45+debian9~1.gbp365209

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
SET time_zone = "+00:00";


CREATE TABLE `vzw_missopportunity` (
  `Agent_ID` varchar(6) NOT NULL,
  `Agent_Name` varchar(31) NOT NULL,
  `Tran_Year` varchar(4) NOT NULL,
  `Tran_Period` varchar(2) NOT NULL,
  `Original_Mobile_ID` varchar(10) DEFAULT NULL,
  `Mobile_ID` varchar(10) NOT NULL,
  `Device_ID` varchar(14) NOT NULL,
  `New_Plan` varchar(5) NOT NULL,
  `Customer_Name` varchar(25) NOT NULL,
  `Device_Change_Date` varchar(10) NOT NULL,
  `New_Plan_Access_Charge` varchar(11) NOT NULL,
  `Model` varchar(32) NOT NULL,
  `Agent_SSO_ID` varchar(7) DEFAULT NULL,
  `Customer_Type` varchar(2) NOT NULL,
  `Previous_Plan` varchar(5) DEFAULT NULL,
  `Previous_Access_Charge` varchar(11) DEFAULT NULL,
  `ID` int(20) NOT NULL,

) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;



INSERT INTO `vzw_missopportunity` (`Agent_ID`, `Agent_Name`, `Tran_Year`, `Tran_Period`, `Original_Mobile_ID`, `Mobile_ID`, `Device_ID`, `New_Plan`, `Customer_Name`, `Device_Change_Date`, `New_Plan_Access_Charge`, `Model`, `Agent_SSO_ID`, `Customer_Type`, `Previous_Plan`, `Previous_Access_Charge`, `ID`, `notes`) VALUES
('123456', ' FALL RIVER', '2019', '11', '5551212555', '5551212555', '35698310000000', '13610', 'PATRICIA ', '11/11/2019', '$100.00', 'SAMSUNG GALAXY A10E', 'testuser12', 'PE', '89850', '$15.00', 1, ''),
('635987', ' NASHUA', '2019', '11', '5551212555', '5551212555', '35698310000000', '13610', 'JANET ', '11/1/2019', '$100.00', 'IPHONE 11 PURPLE 128GB VZ', 'testuser12', 'PE', '91945', '$20.00', 2, ''),
('201258', ' Westford', '2019', '12', '5551212555', '5551212555', '35698310000000', '13610', 'KIM M ', '12/23/2019', '$100.00', 'LG G7 THINQ SILVER', 'testuser12', 'PE', '94156', '$30.00', 3, ''),
('123456', ' Halifax', '2019', '12', '5551212555', '5551212555', '35698310000000', '13610', 'WILBUR L ', '12/18/2019', '$100.00', 'IPHONE XS MAX SILVER 64GB VZ', 'testuser12', 'PE', '94156', '$30.00', 4, ''),
('123456', ' Wilmington', '2019', '11', '5551212555', '5551212555', '35698310000000', '13610', 'RICHARD W ', '11/8/2019', '$100.00', 'IPHONE XR WHITE 64GB', 'testuser12', 'PE', '94156', '$30.00', 5, ''),
('123456', ' Westford', '2019', '10', '5551212555', '5551212555', '35698310000000', '13610', 'DIANE M ', '10/7/2019', '$100.00', 'IPHONE XR CORAL 64GB', 'testuser12', 'PE', '94156', '$30.00', 6, ''),
('123456', ' Halifax', '2019', '10', '5551212555', '5551212555', '35698310000000', '13610', 'KENNETH J ', '10/26/2019', '$100.00', 'KYOCERA CADENCE LTE', 'testuser12', 'PE', '94156', '$30.00', 47, ''),

ALTER TABLE `vzw_missopportunity`
  ADD PRIMARY KEY (`ID`);

ALTER TABLE `vzw_missopportunity`
  MODIFY `ID` int(20) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=3575;
COMMIT;
Josh Eller :

If you want to group by every column in vzw_missopportunity, your group by statement would look like this (also note that you really only want to use select * for quick ad-hoc queries, and never in real code).

Edit: You mentioned you also want to see results without notes. If so, change the inner join on the notes table to a left join.

SELECT x.Agent_ID,
       x.Agent_Name,
       x.Tran_Year,
       x.Tran_Period,
       x.Original_Mobile_ID,
       x.Mobile_ID,
       x.Device_ID,
       x.New_Plan,
       x.Customer_Name,
       x.Device_Change_Date,
       x.New_Plan_Access_Charge,
       x.Model,
       x.Agent_SSO_ID,
       x.Customer_Type,
       x.Previous_Plan,
       x.Previous_Access_Charge,
       x.ID,
       GROUP_CONCAT(y.note ORDER BY y.note_id separator " ") as note
 from
 (
     SELECT A.Agent_ID,
            A.Agent_Name,
            A.Tran_Year,
            A.Tran_Period,
            A.Original_Mobile_ID,
            A.Mobile_ID,
            A.Device_ID,
            A.New_Plan,
            A.Customer_Name,
            A.Device_Change_Date,
            A.New_Plan_Access_Charge,
            A.Model,
            A.Agent_SSO_ID,
            A.Customer_Type,
            A.Previous_Plan,
            A.Previous_Access_Charge,
            A.ID
       from vzw_missopportunity A
       INNER JOIN locations l ON A.Agent_ID = l.Outlet_ID
       WHERE A.Agent_ID = "'.$_SESSION['agent'].'"
       AND l.district = "'.$_SESSION['district'].'"
 ) as x
 left join
 (

  SELECT note, note_id from notes

 ) as y on x.ID = y.note_id
 GROUP BY x.Agent_ID,
          x.Agent_Name,
          x.Tran_Year,
          x.Tran_Period,
          x.Original_Mobile_ID,
          x.Mobile_ID,
          x.Device_ID,
          x.New_Plan,
          x.Customer_Name,
          x.Device_Change_Date,
          x.New_Plan_Access_Charge,
          x.Model,
          x.Agent_SSO_ID,
          x.Customer_Type,
          x.Previous_Plan,
          x.Previous_Access_Charge,
          x.ID

More concisely, I would re-write it like this:

SELECT x.Agent_ID,
       x.Agent_Name,
       x.Tran_Year,
       x.Tran_Period,
       x.Original_Mobile_ID,
       x.Mobile_ID,
       x.Device_ID,
       x.New_Plan,
       x.Customer_Name,
       x.Device_Change_Date,
       x.New_Plan_Access_Charge,
       x.Model,
       x.Agent_SSO_ID,
       x.Customer_Type,
       x.Previous_Plan,
       x.Previous_Access_Charge,
       x.ID,
       y.all_notes
FROM vzw_missopportunity x
INNER JOIN locations l
    ON l.Outlet_ID = x.Agent_ID
LEFT JOIN (
    SELECT n.note_id,
           GROUP_CONCAT(n.note ORDER BY n.note_id separator " ") as all_notes
    FROM notes n
    GROUP BY n.note_id
) AS y
    ON y.note_id = x.ID
WHERE x.Agent_ID = "'.$_SESSION['agent'].'"
  AND l.district = "'.$_SESSION['district'].'"

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=14060&siteId=1