Adding a field in a MySQL Query - php

I need to add a field in one of our query. I'm nt a PHP programmer buy I can get my way around a little. The query is:
if (_QUERYSTRING_) {
switch ($intMode) {
case -1:
$result = mysqli_query($mysql,"
SELECT orders.id,
orders_addresses.strlastname,
orders_addresses.strfirstname,
orders_addresses.intprovince,
9 AS intmode,
Date(orders.dtimeorder) AS datepayment,
orders_costs.dbltotal AS dblamount,
orders_notes.strcod AS strtxn,
0 AS dblfee,
shipping_postalservices.strtracking"._LANG_." as strtrackingurl,
'À recevoir' AS strmode,
NULL AS strvendor
FROM orders
JOIN orders_costs
ON orders_costs.id = orders.id
JOIN orders_addresses
ON orders_addresses.id = orders.id
JOIN orders_notes
ON orders_notes.id = orders.id
JOIN shipping_postalservices
ON shipping_postalservices.id = orders_costs.intpostalservice
WHERE date(orders.dtimeorder) BETWEEN '".date("Y-m-d",$timeStart)."' AND '".date("Y-m-d",$timeEnd)."'
AND orders.boolshipped = 1
AND orders.boolcanceled = 0
AND orders_costs.boolcod = 1
AND orders_costs.dbltotal > 0
AND NOT EXISTS
(
SELECT *
FROM orders_payments
WHERE orders_payments.intorder = orders.id
AND orders_payments.intmode = 9
AND orders_payments.dblamount > 0)
GROUP BY orders.id
ORDER BY orders.dtimeorder,
orders.id");
break;
default:
$result = mysqli_query($mysql,"
SELECT orders.id,
orders_addresses.strlastname,
orders_addresses.strfirstname,
orders_addresses.intprovince,
orders_payments.intmode,
Date(orders_payments.dtimepayment) AS datepayment,
orders_payments.dblamount,
orders_payments.strtxn,
orders_payments.dblfee,
shipping_postalservices.strtracking"._LANG_." as strtrackingurl,
payments.strname"._LANG_." AS strmode,
payments.strvendor
FROM orders_payments
JOIN orders
ON orders.id = orders_payments.intorder
JOIN orders_costs
ON orders_costs.id = orders.id
JOIN orders_addresses
ON orders_addresses.id = orders.id
JOIN shipping_postalservices
ON shipping_postalservices.id = orders_costs.intpostalservice
LEFT JOIN payments
ON payments.id = orders_payments.intmode
WHERE date(orders_payments.dtimepayment) BETWEEN '".date("Y-m-d",$timeStart)."' AND '".date("Y-m-d",$timeEnd)."'".(!empty($intMode) ? "
AND orders_payments.intmode = '".$intMode."'" : NULL)."
GROUP BY orders.id,
orders_payments.intpayment
ORDER BY orders_payments.dtimepayment,
orders.id");
break;
}
The field that needs to be added is orders_addresses.intProvince so it can be displayed in the results. I tried to understand a little but I guess it's a little more complicated than I thought. It does display the province, which are numbers. My question would be, how do I "translate" those numbers by the actual names so it displays "Ontario" instead of 9? The name of the provinces are in another table called Province.

You will need to add another JOIN:
JOIN Province ON orders_addresses.intprovince = Province.x
And in the SELECT part, replace orders_addresses.intprovince by Province.y
where
x = column in table Province that holds the province id
y = column in table Province that holds the province name

Related

Conditionally change a join on column

I have this invalid mysql statement:
UPDATE third_party_raw_stock_price AS r
IF feed_link_column = 'supplier_barcode'
JOIN options_new AS o
ON o.supplier_barcode = r.supplier_option_code
END IF
IF feed_link_column = 'supplier_code'
JOIN options_new AS o
ON o.supplier_code = r.supplier_option_code
END IF
JOIN third_party_config AS c
ON SUBSTRING(o.options_id, 3, 2) = c.code
SET o.price = 9.99, o.cost_price_variation = 3.33, o.stock = 7
LIMIT 2000
How do I rewrite this to dynamically define the options_new join column? I need the join to be either ON o.supplier_code = r.supplier_option_code or ON o.supplier_barcode = r.supplier_option_code, depending on which is called for by the third_party_raw_stock_price.third_party_raw_stock_price column.
A JOIN statement is basically just a boolean test - if the result of the test (however simple or complicated it is) evaluates to true, then the records are joined. If it's false, then there's no join.
That means your join condition can be as arbitarily complex as you need it to be, as long as it boils down to a true/false value in the end:
SELECT ...
FROM bar
JOIN foo ON (foo.feed = 'barcode' AND foo.supplier = bar.supplier)
OR
(foo.feed = 'code' AND foo.code = bar.code)

Mysql Complex Join Query Issue

I have 5 mysql tables as described below.
clinics table
id
name
d_location_subscription table
id
clinic_id
t_id //t_id will contain a foreign key of d_cities, d_states or d_countries table
type "country" "state" "city"
d_countries table
id
name
code
d_states table
id
d_country_id
name
code
d_city table
id
d_state_id
name
code
d_location_subscription table is used to record clinic's subscription for a location(it may be a city, state or country). I'm expecting to get all subscribed cities for a specific
clinic using d_location_subscription table.
For example, if clinic A is subscribed to Texas state, I should be able to get all city ids for clinic A.
I created following sql query, it looks ugly but generate a close result what i want to achieve.
select
`d`.`id` AS `clinic_id`,
if((`dct`.`id` is not null),`dct`.`id`,if((`dct1`.`id` is not null),`dct1`.`id`,`dct2`.`id`)) AS `d_city_id`
from ((((((((
`d_location_subscriptions` `dls`
join `clinics` `d`
on((`d`.`id` = `dls`.`clinic_id`)))
left join `d_countries` `dc`
on(((`dc`.`id` = `dls`.`t_id`) and (`dls`.`type` = 'country'))))
left join `d_states` `ds`
on((`ds`.`d_country_id` = `dc`.`id`)))
left join `d_cities` `dct2`
on((`dct2`.`d_state_id` = `ds`.`id`)))
left join `d_states` `ds1`
on(((`ds1`.`id` = `dls`.`t_id`) and (`dls`.`type` = 'state'))))
left join `d_cities` `dct`
on((`dct`.`d_state_id` = `ds1`.`id`)))
left join `d_cities` `dct1`
on(((`dct1`.`id` = `dls`.`t_id`) and (`dls`.`type` = 'city'))))
)
when there is record with type "country" in d_location_subscription table, I receive following result. total number of records returned are equal to the number of d_states table records.
How should I get rid of those Null values by changing above query?
And please advice me if this is the correct way to acheive similar functionality. Thanks in advance :)
The quickest, dirtiest way to achieve what you want is just to append this where condition to your query:
WHERE d_city_id is not null
but you might prefer to rework your query and decide where you really need LEFT joins and not INNER joins
the IF() computed column is in essence what STT LCU was trying to offer, but you can't use that directly in the where for some reason.
I've rewritten your query, but with different aliases to better follow the origination of the tables / relationships to get the data. In the end, I've added a where to test for ANY ONE of the "ID" values as NOT NULL. If they are ALL Null, the record should be excluded..
select
d.id AS clinic_id,
if(CityViaState.id is not null, CityViaState.id,
if( ByCity.id is not null, ByCity.id, CityViaCountry.id )) AS d_city_id
from
d_location_subscriptions dls
join clinics d
ON dls.clinic_id = d.id
left join d_countries ByCountry
ON dls.t_id = ByCountry.id
and dls.type = 'country'
left join d_states StateViaCountry
ON ByCountry.id = StateViaCountry.d_country_id
left join d_cities CityViaCountry
ON StateViaCountry.id = CityViaCountry.d_state_id
left join d_states ByState
ON dls.t_id = ByState.id
and dls.type = 'state'
left join d_cities CityViaState
ON ByState.id = CityViaState.d_state_id
left join d_cities ByCity
ON dls.t_id = ByCity.id
and dls.type = 'city'
where
CityViaState.id is not null
OR ByCity.id is not null
OR CityViaCountry.id is not null

Query with join query, need assistance

I have three tables.
I combine the company and component tables with this code
$questions_query = "SELECT company_mast.id, component_mast.component_id
FROM company_mast
LEFT JOIN component_mast
ON company_mast.id = component_mast.company_id
WHERE component_mast.component_name = '".$component_name."'
AND company_mast.company_name = '".$company_name."'";
The result is as desired, If I put company_name as Bells and component_name as Assets I get and id of 3 for Bells and an id of 9 for Assets. Now if you look at the customfields table I need to pull all the questions with the a specific company_id and component_id.
Example: If the user enters Bells and Assets they need to receive all questions with the company_id of 3 and the component_id of 9.
So this is my query
SELECT *
FROM customfield_mast
LEFT JOIN ( SELECT company_mast.id, component_mast.component_id
FROM company_mast
LEFT JOIN component_mast
ON company_mast.id = component_mast.company_id
WHERE component_mast.component_name = 'Assets'
AND company_mast.company_name = 'Bells')
att
ON customfield_mast.company_id = customfield_mast.component_id
This however returns all questions in my db, which is not what I want. I'm positive my last "ON" statement is the problem, however I don't know what the correct statement would be. I have not started with SQL injection protection, this is grass roots to get my queries to work. Thanks for the help
What's wrong with another join?
SELECT company_mast.id, component_mast.component_id, CFM.DisplayName
FROM company_mast
LEFT JOIN component_mast
ON company_mast.id = component_mast.company_id
LEFT JOIN CustomField_mast CFM
ON CFM.Company_ID = Component_mast.Company_ID
and CFM.Component_ID = component_Mast.Component_ID
WHERE component_mast.component_name = '".$component_name."'
AND company_mast.company_name = '".$company_name."'";
SELECT * FROM `customfield_mast`
WHERE `company_id` =
(SELECT `id` FROM `company_mast` WHERE `company_name` = '$company_name')
AND `component_id` IN
(SELECT GROUP_CONCAT(`component_id`) FROM `component_mast`
WHERE `component_name` = '$component_name')

How can I filter results from a complex SQL query?

I have a sql query like so:
SELECT `categories`.`partnumbersafe`, `filters`.`filtername`, `filters`.`filtervalue`
FROM `products_categories` AS `categories`
LEFT OUTER JOIN `products` AS `product` ON `categories`.`partnumbersafe` = `product`.`partnumbersafe`
LEFT OUTER JOIN `products_filters` AS `filters` ON `categories`.`partnumbersafe` = `filters`.`partnumbersafe`
WHERE `categories`.`categoryid` =4
AND (`product`.`visibility` =1 OR `product`.`visibility` =2)
AND `product`.`status` =1
This gives me a result where there are multiple partnumbersafe entries (same value) with different filternames and filtervalues. For example:
partnumbersafe filtername filtervalue
123 brand toyota
123 model F5
123 type business
678 brand toyota
678 model F6
The query is generated by PHP. I also have POST values in my PHP script which hold filterdata (brand is toyota, model is F6 for example).
How can I filter these results for filtername/value pairs? I would prefer a sql query solution instead of a php solutiuon, where I can query for partnumbersafe where brand is toyota and model is F6. So that it would only retrieve partnumbersafe 123 in the above table.
The table products_filters holds all the data as represented by the table outline above.
you need to have multiple condition here. The number of record for each partnumbersafe is equal to the number of condition you supplied.
SELECT partnumbersafe
FROM...
WHERE (filterName = 'Brand' AND filtervalue = 'toyota')
OR
(filterName = 'Model' AND filtervalue = 'F6')
GROUP BY partnumbersafe
HAVING COUNT(*) = 2
SELECT
`categories`.`partnumbersafe`
FROM
`products_categories` AS `categories`
LEFT OUTER JOIN
`products` AS `product`
ON `categories`.`partnumbersafe` = `product`.`partnumbersafe`
LEFT OUTER JOIN
`products_filters` AS `filters`
ON `categories`.`partnumbersafe` = `filters`.`partnumbersafe`
WHERE
`categories`.`categoryid` = 4
AND (`product`.`visibility` =1 OR `product`.`visibility` =2)
AND `product`.`status` =1
AND
(
(`filters`.`filtername` = 'Brand' AND `filters`.`filtervalue` = 'Toyota')
OR
(`filters`.`filtername` = 'Model' AND `filters`.`filtervalue` = 'F6')
)
GROUP BY
`categories`.`partnumbersafe`
HAVING
COUNT(*) = 2
This assumes that you only need the partnumbersafe and that no filtername/filtervalue pair would ever apply to the same partnumbersafe more than once.

php: doctrine 2 and innerJoin condition

I have problem with innerJoin.
Two tables range and product:
table range
id
parent_id
category_id
table product
id
range_id
the query must join range.id with range2.chidren only level 1 and range, range2 with product
ex:
range.id = product.range_id or range2.id = product.range_id:
I want something like :
INNER JOIN product p1_ ON p0_.id = p1_.range_id or p4_.id = p1_.range_id
with doctrine when I write :
->innerJoin('r.products', 'p', Expr\Join::WITH, 'r.id = p.range or rp.id = p.range ')
I got :
INNER JOIN product p1_ ON p0_.id = p1_.range_id AND (p0_.id = p1_.range_id OR p4_.id = p1_.range_id)
someone have solution

Categories