Note that there are some explanatory texts on larger screens.

plurals
  1. POMySql Query - select data from 2 tables and replace contents of field
    primarykey
    data
    text
    <p>I am trying to finish an sql query and whilst I have found similar problems solved, I cannot get mine to work.</p> <p>I have two tables, orders and cars (there are more fields but these are the ones I am having trouble with:</p> <p>table_cars<br>------------------<br> ID&nbsp;&nbsp; Title<br></p> <p>1 &nbsp;&nbsp;&nbsp; Blue car <br> 2 &nbsp;&nbsp;&nbsp; Red Car<br> 3 &nbsp;&nbsp;&nbsp; Green Car</p> <p>table_orders<br>-----------------<br> Vehicletype<br></p> <p>2 <br> 2 <br> 1 <br> 3 <br> 1 <br> 2 <br></p> <p>So I have the following code, but what I need is the correct inner join syntax to replace the output of vehicle type from the ID to the title as defined in the cars table, so instead of saying '2' it will say 'red car'</p> <p>Here is what I have for the select query:</p> <pre><code>SELECT names, phone, cprice, vehicletype FROM table_orders WHERE driver_id = {$UserId} </code></pre> <p>The results are already filtered on driver_id.</p> <p>Thanks in advance for any help :)</p> <p>--------------------------Continued-----------------------------------------------</p> <p>Apologies, I did get it to work I was missing the 's' after the table name.You seem very smart so I am going to ask another question if that is ok. Here is my full query so far which I know is messy! I am going to use 2 more inner joins to apply the same solution to the 'state' and 'payment' fields (can I have 3?) but my question is on the UNIXTIME fields.</p> <p>The data is stored like 1384380000 in the table so I have to use the UNIXTIME with formatting to get the data to display how I want it. What I want though is the 'datetime2' field will sometimes be empty, so I want to it return either a blank field or a zero if it is empty as currently it returns a date and time like 01/01/70 01:00:00.</p> <pre><code>SELECT s.names, s.phone, IF(s.returntrip=0, 'No', 'Yes'), s.cprice, s.prepay, s.selpassengers, tc.title AS Vehicle, s.selinfantseats, s.selchildseats, s.selboosterseats, s.selluggage, s.selcarry, s.PickupAddr1, s.PickupAddr4, FROM_UNIXTIME(datetime1,'%d/%m/%y %T'), FROM_UNIXTIME(datetime2,'%d/%m/%y %T'), s.message, s.begin, s.end, (CASE WHEN state = '1' THEN 'Accepted' WHEN state = '-2' THEN 'Waiting' WHEN state = '-1' THEN 'Archived' WHEN state = '0' THEN 'Rejected' ELSE 'Unknown' END) AS Status, (CASE WHEN payment = '4' THEN 'Cash to Driver' WHEN payment = '5' THEN 'Credit/Debit Card' WHEN payment = '6' THEN 'Credit/Debit Card to Driver' WHEN payment = '7' THEN 'Account' ELSE 'Unknown' END) AS Payment, s.driver_id FROM #__taxibooking_orders s INNER JOIN #__taxibooking_cars tc ON tc.id = s.vehicletype WHERE s.driver_id = {$UserId} </code></pre> <p>Thanks very much indeed :)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload