Link to home
Start Free TrialLog in
Avatar of prowebinteractiveinc
prowebinteractiveinc

asked on

difficulty building mysql query to get the result Im looking for

My tables: companies, paymentTypes, paymentProcessors, companiesHasPayProcessors

I have a select box where you choose the payment type
Cash
Cheque
Paypal
Credit Card

in this particular case the companiesHasPayProcessors has the records meaning works with 3 different processors, so basically I should have:
Cash
Cheque
Paypal
Credit Card (Internet Secure)
Credit Card (Moneris)
Credit Card (Desjardins Merchant Services)

My current query is:
SELECT pt.PaymentType, pt.display, p.processorName
FROM paymentTypes AS pt,
processors AS p                  
INNER JOIN companyProcessorsAffiliation AS cpa ON p.processorId = cpa.processorId
WHERE cpa.companyId = 23 ORDER BY pt.PaymentType

attached image is what it gives me:

my exact mySQL structure:
--
-- Table structure for table `companies`
--

CREATE TABLE IF NOT EXISTS `companies` (
  `companyId` int(9) unsigned NOT NULL auto_increment,
  `companyLoginId` varchar(9) NOT NULL,
  `customerSince` timestamp NOT NULL default CURRENT_TIMESTAMP,
  `primaryContactUserId` int(9) NOT NULL,
  `companyName` varchar(50) NOT NULL,
  `companyWebsite` varchar(75) NOT NULL,
  `companyStatusId` int(2) NOT NULL,
  PRIMARY KEY  (`companyId`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=24 ;

-- --------------------------------------------------------

--
-- Table structure for table `companyProcessorsAffiliation`
--

CREATE TABLE IF NOT EXISTS `companyProcessorsAffiliation` (
  `cpaId` int(9) unsigned NOT NULL auto_increment,
  `companyId` int(9) NOT NULL,
  `processorId` int(4) NOT NULL,
  PRIMARY KEY  (`cpaId`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=4 ;

-- --------------------------------------------------------

--
-- Table structure for table `paymentTypes`
--

CREATE TABLE IF NOT EXISTS `paymentTypes` (
  `paymentTypeId` int(2) unsigned NOT NULL auto_increment,
  `display` varchar(25) NOT NULL,
  `PaymentType` varchar(25) NOT NULL,
  PRIMARY KEY  (`paymentTypeId`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=5 ;

-- --------------------------------------------------------

--
-- Table structure for table `processors`
--

CREATE TABLE IF NOT EXISTS `processors` (
  `processorId` int(4) unsigned NOT NULL auto_increment,
  `paymentTypeId` int(2) NOT NULL,
  `processorName` varchar(35) NOT NULL,
  PRIMARY KEY  (`processorId`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=5 ;

Open in new window

Please help, Thanks in advance
mySQL-result.jpg
Avatar of Lee Wadwell
Lee Wadwell
Flag of Australia image

You have not specified join criteria between paymentTypes and processors ... based on the table definitions I would have expected:
SELECT pt.PaymentType, pt.display, p.processorName
FROM paymentTypes AS pt
INNER JOIN processors AS p ON pt.paymentTypeId = p.paymentTypeId
INNER JOIN companyProcessorsAffiliation AS cpa ON p.processorId = cpa.processorId
WHERE cpa.companyId = 23 ORDER BY pt.PaymentType

Open in new window

is this the problem or is there something else you needed?
Avatar of prowebinteractiveinc
prowebinteractiveinc

ASKER

as soon as I put any type of join I ssem to get
Credit Card (Internet Secure)
Credit Card (Moneris)
Credit Card (Desjardins Merchant Services)

Instead of
Cash
Cheque
Paypal
Credit Card (Internet Secure)
Credit Card (Moneris)
Credit Card (Desjardins Merchant Services)

I need the rest of the results in the paymentTypes table
anybody ?
Sorry ... my bed distracted me.

You seem to have asked another question the same as this one:
https://www.experts-exchange.com/questions/27866771/Need-help-building-mysql-query.html

But in that one you have added the data - which helps.

The data shows the that the company 23 has only 3 rows in `companyProcessorsAffiliation` to link them to `processors` (processorId 1, 2 & 3).  These three `processors` link to the 3 records in `paymentTypes` for Paypal, Elavon (Internet Secure) & Moneris respectively.

In fact - there is NO link from `processors` to `paymentTypes` for Cash or Cheque at all.

So I do not see how to produce the result you want from the data provided.
I have tried to link them and the cash, cheque, paypal disappears ?
my last query I tried was

SELECT p.processorName, pt.paymentType, pt.display
FROM paymentTypes AS pt
LEFT JOIN processors AS p ON
pt.paymentTypeId=p.paymentTypeId
INNER JOIN companyProcessorsAffiliation AS cpa
ON p.processorId = cpa.processorId
WHERE cpa.companyId = 23
ORDER BY pt.PaymentType

which gives me:
Credit Card (Internet Secure)
Credit Card (Moneris)
Credit Card (Desjardins Merchant Services)

missing from paymentTypes table:
Cash
Cheque
Paypal
The only way, based on how I understand the data structures, is to add rows into the `companyProcessorsAffiliation` and `processors` tables.
do you know of a better way as for the structure, please tell me how you would do it
No, the structure is OK.  I do now know enough (anything actually) about you business to think of a better one.  But based on it, I would think that it should be populated like below to product the result you want:

INSERT INTO `companies` (`companyId`, `customerSince`, `primaryContactUserId`, `companyName`) VALUES
(23, '2012-09-13 14:29:44', 7, 'PROWEBINTERACTIVE INC');

INSERT INTO `companyProcessorsAffiliation` (`cpaId`, `companyId`, `processorId`) VALUES
(1, 23, 1),
(2, 23, 2),
(3, 23, 3),
(3, 23, 4),
(3, 23, 5),
(3, 23, 6);

INSERT INTO `processors` (`processorId`, `paymentTypeId`, `processorName`) VALUES
(1, 3, 'Paypal'),
(2, 4, 'Elavon (Internet Secure)'),
(3, 4, 'Moneris'),
(4, 4, 'Desjardins'),
(5, 1, 'Cash'),
(6, 2, 'Cheque');

INSERT INTO `paymentTypes` (`paymentTypeId`, `display`, `PaymentType`) VALUES
(1, 'ccgroupbox', 'Cash'),
(2, 'ccgroupbox', 'Cheque'),
(3, 'ccgroupbox', 'Paypal'),
(4, 'ccgroupbox', 'Credit Card');
so no query can get what I want with the current structure as is ?
The structure ... i.e. table definitions and relationships ... is fine.  It can produce the results you want - with the right data stored in the tables.  Data is not structure.
right. the part Im not liking is cash and cheque and maybe any future other types I may add are not processors...
what is 'processors' by the definition of your data model?
Again ... I state I do not know your business, so it is very hard for me to guess the meanings.
What is company for example ... is it the organisation that makes payments via the defined methods ... or can receive payments via those methods ... or handles the payments via those methods for others.   By the structure - I could interpret any of those 3 options.
processors are companies that process credit card payments
so I have a table of just those. then
the payment type, when a payment is recorded, the system requires to select the payment type, it is unusual that a company has more then one merchant account but its requested so I know to make it available so if lets say I use Elavon, Moneris, and Desjardins to process my credit cards. I want to give the choice in the select box

so again based on a company dealing with x-amount of payment processors the selectbox should show
Cash
Cheque
Paypal => should work the same as credit cards actually, Im going to change as processor
Credit Card (Elavon)
Credit Card (Moneris)
Credit Card (desjardins)

so now they make their selection (if its credit card, not cash or cheque) they can enter the credit card info and it will be processed with the selected merchant account.

Thats why I have built it as is... Im sure there must be some query to get the info I want...

Thanks
Will cash and cheque 'always' be available choices for every company?
ASKER CERTIFIED SOLUTION
Avatar of Lee Wadwell
Lee Wadwell
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
paymentTypes could have one to many.. if anything maybe an extra field would need to be processors ?
>>paymentTypes could have one to many
one to many what?
it is an existence flag ... it is not a foreign key.
A1 you deserve the extra 500 so just reply to the other one with the link to this solution so I can give you the points. thanks
That is OK.  Please delete the other one ... I do not need the points, nor is it why I try to help on EE.