Categories: php, mysql, sql

how can select 3 records of foreign key with join in sql

2 answers

I'm sorry for my poor English . I have a table for project registration : like

Type of service | Total Amount | prepayment | last payment  | Cost of tolls     web design   |      1000    |  1 (F-key) |  2 (F-key)    |   3 (F-key) 

The second table for the status of payment and amount

  id   |  amount  |   Payment status  |    Description   1    |   400    |        yes        |   for prepayment     2    |   600    |        no         |   for last payment    3    |    0     |        no         |   for Cost of tolls  

Now my question is : How can I join two tables And pull out all THAT three columns ?

I've pulled out the prepayment column with this code

SELECT mzmx_request.service, mzmx_request.amount, (mzmx_payment.payment) as prepayment, (mzmx_payment.status) as prepayment_status  FROM mzmx_request JOIN mzmx_payment ON mzmx_request.prepayment = mzmx_payment.id 

How do I add two more columns?

JOIN mzmx_payment ON mzmx_request.payment = mzmx_payment.id JOIN mzmx_payment ON mzmx_request.pay_more = mzmx_payment.id 

All answers to this question, which has the identifier 61237920

The best answer:

Just join three times. You already did it once. For example:

SELECT   r.service,    r.amount,   pp.payment as prepayment,   pp.status as prepayment_status,   lp.payment as last_payment,   lp.status as last_payment_status,   ct.payment as cost_of_tolls,   ct.status as cost_of_tolls_status  FROM mzmx_request r left JOIN mzmx_payment pp ON r.prepayment = pp.id left JOIN mzmx_payment lp ON r.lastpayment = lp.id left JOIN mzmx_payment ct ON r.costoftolls = ct.id 

Since you are joining three times against the same table you'll need to alias the table. In this case I used the aliases pp, lp, and ct for prepayment, last payment, and cost of tolls respectively.

3 referenced fields are independent - so they needs in independent ref table copies:

SELECT * FROM main JOIN slave AS slave1 ON main.prepayment    = slave1.id JOIN slave AS slave2 ON main.last_payment  = slave2.id JOIN slave AS slave3 ON main.cost_of_tolls = slave3.id  

Last questions

how do i remove the switch on my home screen?
how to edit the JS date and time to update atuomatically?
How to utilize data stored in a multidimensional array
Powermockito not mocking URL constructor in URI.toURL() method
Android Bluetooth LE Scanner only scans when phone's Location is turned on in some devices
docker wordpress container can't connect to mysql container
How can I declare a number in java that is more than 64-bits? [duplicate]
Optaplanner solutionClass entityCollectionProperty should never return null error when simple JSON object passed to controller
Anylogic, get the time a pedestrain is in a queue
How do I fix this syntax issue with my .flex file?
Optimizing query in PHP
How to find the highest number of a column and print two columns of that row in R?
Ideas on “Error: Type com.google.firebase.iid.zzav is referenced as an interface from com.google.firebase.messaging.zzd”?
JCIFS SmbFile.exists() and SmbFile.isDirectory() return false when it exists and I can listFiles()
PHP total order
Laravel booking system design
neural net - undefined column selected
How to indicate y axis does not start from 0 in ggplot?
Fragments in backStack
Spinner how to change the data