Mysql Join Question - php

What is incorrect?
I cant get any data...
SELECT
my_site_orders.order_id,
my_site_orders.order_orderer_id,
my_site_orders.order_ip,
my_site_orders.order_time,
my_site_orders.order_campaign_id,
my_site_orders.order_status,
my_site_orders.order_shipment_status,
my_site_orders.order_shipping_method,
my_site_orders.order1_id,
my_site_orders.order1_coupon_id,
my_site_orders.order1_amount,
my_site_orders.order1_price,
my_site_orders.order_cargo_name,
my_site_orders.order_cargo_surname,
my_site_orders.order_cargo_vatnumber,
my_site_orders.order_cargo_line1,
my_site_orders.order_cargo_line2,
my_site_orders.order_cargo_stateprovince,
my_site_orders.order_cargo_district,
my_site_orders.order_cargo_city,
my_site_orders.order_cargo_contactphone,
my_site_orders.order_invoice_name,
my_site_orders.order_invoice_surname,
my_site_orders.order_invoice_company,
my_site_orders.order_invoice_line1,
my_site_orders.order_invoice_line2,
my_site_orders.order_invoice_vatnumber,
my_site_orders.order_invoice_vat,
my_site_orders.order_invoice_stateprovince,
my_site_orders.order_invoice_district,
my_site_orders.order_invoice_city,
my_site_orders.order_invoice_contactphone,
my_site_orders.order_type,
my_site_orders.order_note,
my_site_orders.order_hash,
my_site_orders.order_bank_hash,
my_site_orders.order_phone_number,
my_site_orders.order_price,
my_site_orders.order_cc_name,
my_site_orders.order_instalment_amount,
my_site_orders.order_instalment_price,
my_site_users.user_name,
my_site_users.user_email,
my_site_orders.order1_type,
my_site_users.user_realname,
my_site_users.user_surname,
my_site_products.product_brand_id,
my_site_products.product_id,
my_site_products.product_cat_id,
my_site_products.product_title,
my_site_products.product_snippet,
my_site_products.product_description,
my_site_products.product_code,
my_site_products.product_normalprice,
my_site_products.product_price,
my_site_products.product_quantity,
my_site_products.product_image1,
my_site_products.product_image1_t1,
my_site_products.product_image4,
my_site_products.product_image4_t1,
my_site_products.product_image4_t2,
my_site_products.product_image_big4,
my_site_products.product_image5,
my_site_products.product_image5_t1,
my_site_products.product_image5_t2,
my_site_products.product_image_big5,
my_site_products.product_select_choice_title,
my_site_products.product_select_option_title,
my_site_products.product_select1,
my_site_products.product_select2,
my_site_products.product_select3,
my_site_products.product_select4,
my_site_products.product_select5,
my_site_products.product_select6,
my_site_products.product_select7,
my_site_products.product_select8,
my_site_products.product_select9,
my_site_products.product_select10,
my_site_brands.brand_name,
my_site_product_cats.product_cat_name
FROM
my_site_orders
INNER JOIN my_site_users ON (my_site_orders.order_orderer_id = my_site_users.user_id)
INNER JOIN my_site_products ON (my_site_orders.order1_id = my_site_products.product_id)
INNER JOIN my_site_brands ON (my_site_products.product_brand_id = my_site_brands.brand_id)
INNER JOIN my_site_product_cats ON (my_site_products.product_cat_id = my_site_product_cats.product_cat_id)
WHERE
my_site_orders.order1_type = my_site_products.product_select1
OR
my_site_orders.order1_type = my_site_products.product_select2
OR
my_site_orders.order1_type = my_site_products.product_select3
OR
my_site_orders.order1_type = my_site_products.product_select4
OR
my_site_orders.order1_type = my_site_products.product_select5
OR
my_site_orders.order1_type = my_site_products.product_select6
OR
my_site_orders.order1_type = my_site_products.product_select7
OR
my_site_orders.order1_type = my_site_products.product_select8
OR
my_site_orders.order1_type = my_site_products.product_select9
OR
my_site_orders.order1_type = my_site_products.product_select10
But i get the data with:
SELECT
my_site_orders.order_id,
my_site_orders.order_orderer_id,
my_site_orders.order_ip,
my_site_orders.order_time,
my_site_orders.order_campaign_id,
my_site_orders.order_status,
my_site_orders.order_shipment_status,
my_site_orders.order_shipping_method,
my_site_orders.order1_id,
my_site_orders.order1_coupon_id,
my_site_orders.order1_amount,
my_site_orders.order1_price,
my_site_orders.order_cargo_name,
my_site_orders.order_cargo_surname,
my_site_orders.order_cargo_vatnumber,
my_site_orders.order_cargo_line1,
my_site_orders.order_cargo_line2,
my_site_orders.order_cargo_stateprovince,
my_site_orders.order_cargo_district,
my_site_orders.order_cargo_city,
my_site_orders.order_cargo_contactphone,
my_site_orders.order_invoice_name,
my_site_orders.order_invoice_surname,
my_site_orders.order_invoice_company,
my_site_orders.order_invoice_line1,
my_site_orders.order_invoice_line2,
my_site_orders.order_invoice_vatnumber,
my_site_orders.order_invoice_vat,
my_site_orders.order_invoice_stateprovince,
my_site_orders.order_invoice_district,
my_site_orders.order_invoice_city,
my_site_orders.order_invoice_contactphone,
my_site_orders.order_type,
my_site_orders.order_note,
my_site_orders.order_hash,
my_site_orders.order_bank_hash,
my_site_orders.order_phone_number,
my_site_orders.order_price,
my_site_orders.order_cc_name,
my_site_orders.order_instalment_amount,
my_site_orders.order_instalment_price,
my_site_users.user_name,
my_site_users.user_email,
my_site_orders.order1_type,
my_site_users.user_realname,
my_site_users.user_surname,
my_site_products.product_brand_id,
my_site_products.product_id,
my_site_products.product_cat_id,
my_site_products.product_title,
my_site_products.product_snippet,
my_site_products.product_description,
my_site_products.product_code,
my_site_products.product_normalprice,
my_site_products.product_price,
my_site_products.product_quantity,
my_site_products.product_image1,
my_site_products.product_image1_t1,
my_site_products.product_image4,
my_site_products.product_image4_t1,
my_site_products.product_image4_t2,
my_site_products.product_image_big4,
my_site_products.product_image5,
my_site_products.product_image5_t1,
my_site_products.product_image5_t2,
my_site_products.product_image_big5,
my_site_products.product_select_choice_title,
my_site_products.product_select_option_title,
my_site_products.product_select1,
my_site_products.product_select2,
my_site_products.product_select3,
my_site_products.product_select4,
my_site_products.product_select5,
my_site_products.product_select6,
my_site_products.product_select7,
my_site_products.product_select8,
my_site_products.product_select9,
my_site_products.product_select10,
my_site_brands.brand_name,
my_site_product_cats.product_cat_name
FROM
my_site_orders
INNER JOIN my_site_users ON (my_site_orders.order_orderer_id = my_site_users.user_id)
INNER JOIN my_site_products ON (my_site_orders.order1_id = my_site_products.product_id)
INNER JOIN my_site_brands ON (my_site_products.product_brand_id = my_site_brands.brand_id)
INNER JOIN my_site_product_cats ON (my_site_products.product_cat_id = my_site_product_cats.product_cat_id)

First, you can use IN() instead of so many ORs. It will render your query shorter, and easier for reading. Next, obviously you have no orders with order1_type IN(product_select1,product_select2,product_select3,product_select4,product_select5,product_select6,product_select7,product_select8,product_select9,product_select10). Some sample data (from the first query) could help us to rewrite your query correctly

Related

How to add join with substring in propel

I have this following SQL Query
SELECT *
FROM inventory_audits
WHERE id in (
SELECT distinct inventory_audits.id
FROM inventory_audits
LEFT JOIN pick_lists ON (pick_lists.number = substr(inventory_audits.reference,0,8))
LEFT JOIN pick_list_items ON (pick_list_items.pick_list_id = pick_lists.id)
LEFT JOIN load_plan_items ON (load_plan_items.load_plan_id = pick_list_items.load_plan_id)
LEFT JOIN sales_order_items ON (load_plan_items.sales_order_item_id = sales_order_items.id)
LEFT JOIN invoices ON (invoices.id = sales_order_items.invoice_id)
WHERE inventory_adjustment_reason_description = 'Picked'
AND (invoices.number = '230367' OR invoices.number = '230368')
)
And, I need to convert this to propel which results as
$cr = new Criteria();
$cr->addJoin(PickListPeer::ID, PickListItemPeer::PICK_LIST_ID);
$cr->addJoin(PickListItemPeer::LOAD_PLAN_ID, LoadPlanItemPeer::LOAD_PLAN_ID);
$cr->addJoin(LoadPlanItemPeer::SALES_ORDER_ITEM_ID, SalesOrderItemPeer::ID);
$cr->addJoin(SalesOrderItemPeer::INVOICE_ID, InvoicePeer::ID);
$cr->add(InventoryAuditPeer::INVENTORY_ADJUSTMENT_REASON_DESCRIPTION, 'Picked');
$cr->add(InvoicePeer::NUMBER, $invoiceNumbers, Criteria::IN);
$cr->setDistinct(true);
But I'm kinda confused how I can convert this join in propel...
LEFT JOIN pick_lists ON (pick_lists.number =
substr(inventory_audits.reference,0,8))
, I tried this but not working..
$cr->add(PickListPeer::NUMBER, substr(InventoryAuditPeer::REFERENCE,
0, 8));
I am using propel 1.2.1
Any references are appreciated :) tyvm..
Nvm, I got it working by using this line..
$cr->addJoin('substr(inventory_audits.REFERENCE, 0, 8)', PickListPeer::NUMBER, Criteria::LEFT_JOIN);

How can I join 3 tables in SQL

Here is the query I created :
$query = "SELECT address_details.company, address_details.po_box,
address_details.town, letter_details.address_id,
letter_details.attn, letter_details.create_date,
letter_details.ref_no, letter_details.refference,
letter_details.letter_body, letter_details.print_date
FROM letter_details
join address_details ON address_details.id = letter_details.address_id
join signatories ON (letter_details.id = signatories.id)
WHERE letter_details.id='" .$_GET['id'] . "'";
Try using this query.
$query = "SELECT ad.company, ad.po_box,
ad.town, ld.address_id,
ld.attn, ld.create_date,
ld.ref_no, ld.refference,
ld.letter_body, ld.print_date
FROM letter_details ld
left join address_details ad ON (ad.id = ld.address_id)
left join signatories s ON (ld.id = s.id)
WHERE ld.id='" .$_GET['id'] . "'";
First, you should alias for better readibility,
check if you get something in $_GET[id]
echo $_GET[id];
then assign it, (or you may set it as well, if you're using a class)
$fltr = $_GET[id];
And, try to use LEFT JOIN ;
SELECT b.company, b.po_box,
b.town, a.address_id,
a.attn, a.create_date,
a.ref_no, a.refference,
a.letter_body, a.print_date
FROM letter_details AS a LEFT JOIN address_details AS b ON b.id = a.address_id
LEFT JOIN signatories AS c ON (a.id =c.id)
WHERE a.id='$fltr'

How do I nest joins inside a from statement in Codeigniter

Okay, I've been tasked with rewriting a small, old PHP app in Codeigniter, and I ran into a bit a road bump. I'm not sure how to go about handling the joins in the query.
FROM(
(
(
(
(mobiledoc.labdata labdata JOIN mobiledoc.items items
ON (labdata.ItemId = items.itemID)
) JOIN mobiledoc.enc enc
ON (labdata.EncounterId = enc.encounterID)
) JOIN mobiledoc.users users_patient
ON (users_patient.uid = enc.patientID)
) JOIN mobiledoc.users users_Provider
ON (users_Provider.uid = enc.doctorID)
) JOIN mobiledoc.facilitygroupmembers facilitygroupmembers
ON (enc.facilityId = facilitygroupmembers.FacilityId)
)
And then after that FROM there are a few more joins, which I think would be fairly easy.
JOIN mobiledoc.facilitygroups facilitygroups ON (facilitygroups.Id = acilitygroupmembers.GroupId)
JOIN mobiledoc.patients patients ON (enc.patientID = patients.pid)
Any help would be greatly appreciated.
UPDATE:
I decided to just put the nested joins inside the for statement, and move on. Here's the original query.
$result = mysql_query("SELECT patients.ControlNO AS PatientID, users_patient.ulname AS patulname,
users_patient.ufname AS patufname, users_patient.uminitial AS patuminitial, users_patient.dob AS
patdob, users_Provider.ulname AS ULname, users_Provider.ufname AS UFname, items.itemName,
labdata.Notes,enc.date, enc.startTime FROM(((((mobiledoc.labdata labdata JOIN mobiledoc.items
items ON (labdata.ItemId = items.itemID)) JOIN mobiledoc.enc enc ON (labdata.EncounterId =
enc.encounterID)) JOIN mobiledoc.users users_patient ON (users_patient.uid = enc.patientID)) JOIN
mobiledoc.users users_Provider ON (users_Provider.uid = enc.doctorID)) JOIN
mobiledoc.facilitygroupmembers facilitygroupmembers ON (enc.facilityId =
facilitygroupmembers.FacilityId)) JOIN mobiledoc.facilitygroups facilitygroups ON
(facilitygroups.Id = facilitygroupmembers.GroupId) JOIN mobiledoc.patients patients ON
(enc.patientID = patients.pid) WHERE (facilitygroups.Name = '". $_POST['facility_id'] . "') AND
(items.itemName LIKE '%X RAY%' OR items.itemName LIKE '%Cast%' OR items.itemName LIKE '%Splint%'
OR items.itemName LIKE '%DEXA%') AND (enc.VisitType NOT IN ('', 'MT', 'TEL')) AND (enc.`date` =
'" . $_POST['txtYear'] . "-" . $_POST['txtMonth'] . "-" . $_POST['txtDay'] ."') ORDER BY
enc.startTime ASC") or die ("could not execute query!");
Here's the new query:
$this->db->select('patients.ControlNO as id');
$this->db->select('users_patient.ulname as lastName');
$this->db->select('users_patient.ufname as firstName');
$this->db->select('users_patient.uminitial as mInitial');
$this->db->select('users_patient.dob as dob');
$this->db->select('users_Provider.ulname as phys_lastName');
$this->db->select('items.itemName');
$this->db->select('labdata.notes');
$this->db->select('enc.date');
$this->db->select('enc.startTime');
$this->db->from('((((mobiledoc.labdata labdata JOIN mobiledoc.items items ON (labdata.ItemId
= items.itemID)) JOIN mobiledoc.enc enc ON (labdata.EncounterId = enc.encounterID)) JOIN
mobiledoc.users users_patient ON (users_patient.uid = enc.patientID)) JOIN mobiledoc.users
users_Provider ON (users_Provider.uid = enc.doctorID)) JOIN mobiledoc.facilitygroupmembers
facilitygroupmembers ON (enc.facilityId = facilitygroupmembers.FacilityId)) JOIN
mobiledoc.facilitygroups facilitygroups ON (facilitygroups.Id = facilitygroupmembers.GroupId)
JOIN mobiledoc.patients patients ON (enc.patientID = patients.pid)');
$this->db->where($where_array);
$this->db->like($like_array);
$this->db->or_like($or_like_array);
$this->db->where_not_in('enc.VisitType', $not_in);
$this->db->order_by('enc.startTime', 'asc');
$this->db->get();
And the output from that query:
SELECT `patients`.`ControlNO` as id, `users_patient`.`ulname` as lastName,
`users_patient`.`ufname` as firstName, `users_patient`.`uminitial` as mInitial,
`users_patient`.`dob` as dob, `users_Provider`.`ulname` as phys_lastName, `items`.`itemName`,
`labdata`.`notes`, `enc`.`date`, `enc`.`startTime` FROM ((((((mobiledoc.labdata labdata JOIN
mobiledoc.items items ON (labdata.ItemId = items.itemID)) JOIN mobiledoc.enc enc ON
(labdata.EncounterId = enc.encounterID)) JOIN mobiledoc.users users_patient ON (users_patient.uid
= enc.patientID)) JOIN mobiledoc.users users_Provider ON (users_Provider.uid = enc.doctorID))
JOIN mobiledoc.facilitygroupmembers facilitygroupmembers ON (enc.facilityId =
facilitygroupmembers.FacilityId)) JOIN mobiledoc.facilitygroups facilitygroups ON
(facilitygroups.Id = facilitygroupmembers.GroupId) JOIN mobiledoc.patients patients ON
(enc.patientID = patients.pid)) WHERE `facilitygroups`.`Name` = 0 AND `enc`.`date` = '12/05/2014'
AND `enc`.`VisitType` NOT IN ('', 'MT', 'TEL') AND `items`.`itemName` LIKE '%X RAY%' OR
`items`.`itemName` LIKE '%DEXA%' OR `0` LIKE '%items.itemName%' ORDER BY `enc`.`startTime` asc
With CI's active record you can easily join tables by using the following format
$qry = $this->db->select('*')
->from('table1')
->where('table1.id', 1)
->join('table2','table2.t_id = table1.id')
->get();
For more information take a look at CI's active record documentation
Also you can specify the join type by adding a third parameter to the join() function
$this->db->join('table3', 'table3.id = table2.t_id', 'left');

Query within a while loop

I have this query call:
$currplayer = 1;
$query = "SELECT f.fk_user_id2, u.username, u.profileimg FROM ".$prefix."_friends f
INNER JOIN ".$prefix."_users u
ON f.fk_user_id2 = u.id
WHERE f.fk_user_id1 = $currplayer ORDER BY u.username ASC";
if ($result = $mysqli->query($query)){
while ($row1 = $result->fetch_assoc()){
$friendid = $row1['fk_user_id2'];
$sql = $mysqli->query("SELECT * FROM ".$prefix."_newversus WHERE fk_player=$currplayer AND fk_opponent=$friendid");
$row = $sql->fetch_assoc();
I am trying to figure out how I get the query within the loop into the first query with inner join, but can't figure this out.
hoping for help and thanks in advance :-)
If i got you corrrectly, you want to remove the query which is running in loop & add it to main query.
Try this:
SELECT f.fk_user_id2, u.username, u.profileimg FROM ".$prefix."_friends f
INNER JOIN ".$prefix."_users u ON f.fk_user_id2 = u.id
INNER JOIN ".$prefix."_newversus n ON f.fk_user_id2 = n.fk_opponent
WHERE f.fk_user_id1 = $currplayer AND
n.fk_player=$currplayer
ORDER BY u.username ASC;

Set variable from alias in SQL query

I have a simple JOIN query:
$lstCheck = $dbWHMCS->query('SELECT * FROM tblmonitorports mp
INNER JOIN tblmonitorhosts h ON h.hst_id = port_mon
INNER JOIN tblhosting ho ON ho.id = h.hst_serverid
INNER JOIN tblclients cl ON cl.id = ho.userid');
while ($data = $lstCheck->fetch())
{
$serveridx = $data['ho.id'];
$clientid = $data['cl.id'];
}
My problem is that I have an "id" column in both the tblhosting and tblclients tables, so my variables both have the same id. I tried to set it using an alias in the example above (ho. and cl.) but it doesn't work. How can I do this in the example above?
Thank you!
How about this? (a bit rusty on php details, but this should do it):
$lstCheck = $dbWHMCS->query('SELECT ho.id hid, cl.id cid FROM tblmonitorports mp
INNER JOIN tblmonitorhosts h ON h.hst_id = port_mon
INNER JOIN tblhosting ho ON ho.id = h.hst_serverid
INNER JOIN tblclients cl ON cl.id = ho.userid');
while ($data = $lstCheck->fetch())
{
$serveridx = $data['hid'];
$clientid = $data['cid'];
}
You are selecting the records, with a wild card *, so you can't select the fields like that.
As per your query h.hst_serverid & ho.userid have the exact same value as you want. SO simply do this
while ($data = $lstCheck->fetch())
{
$serveridx = $data['hst_serverid'];
$clientid = $data['userid'];
}
However, selecting specific rows might sound better too
$lstCheck = $dbWHMCS->query('SELECT ho.id hid, cl.id cid, ....');
while ($data = $lstCheck->fetch())
{
$serveridx = $data['hid'];
$clientid = $data['cid'];
}

Categories