MySql query duplicating results (with certain parameters) - php

I have a fairly complex query (to me) that returns duplicate results ONLY when the parameter passed results in 'dest_xdock: ' . $row['dest_xdock'] as a certain value. Can anyone identify why this is happening in the Query? I don't have duplicates of this in the table that I can see. Here is the query:
select
`orig_lookup`.`zipcode` AS `orig_zipcode`,
`orig_lookup`.`state` AS `orig_state`,
`orig_lookup`.`svcnotes` AS `orig_svcnotes`,
`orig_lookup`.`svcterms` AS `orig_svcterms`,
`orig_lookup`.`daysout` AS `orig_daysout`,
`orig_lookup`.`daysin` AS `orig_daysin`,
`orig_lookup`.`pcity` AS `orig_city`,
`orig_lookup`.`scac` AS `orig_scac`,
`orig_lookup`.`xdock` AS `orig_xdock`,
`orig_lookup`.`scac2` AS `orig_scac2`,
`orig_lookup`.`xdock2` AS `orig_xdock2`,
`orig_lookup`.`scac3` AS `orig_scac3`,
`orig_lookup`.`xdock3` AS `orig_xdock3`,
`orig_ssas`.`ssa` AS `orig_ssa`,
`orig_hcas`.`hca` AS `orig_hca`,
`dest_lookup`.`zipcode` AS `dest_zipcode`,
`dest_lookup`.`state` AS `dest_state`,
`dest_lookup`.`svcnotes` AS `dest_svcnotes`,
`dest_lookup`.`svcterms` AS `dest_svcterms`,
`dest_lookup`.`daysout` AS `dest_daysout`,
`dest_lookup`.`daysin` AS `dest_daysin`,
`dest_lookup`.`pcity` AS `dest_city`,
`dest_lookup`.`scac` AS `dest_scac`,
`dest_lookup`.`xdock` AS `dest_xdock`,
`dest_lookup`.`scac2` AS `dest_scac2`,
`dest_lookup`.`xdock2` AS `dest_xdock2`,
`dest_lookup`.`scac3` AS `dest_scac3`,
`dest_lookup`.`xdock3` AS `dest_xdock3`,
`dest_ssas`.`ssa` AS `dest_ssa`,
`dest_hcas`.`hca` AS `dest_hca`,
`tbl_matx_20160321`.`trmnljoin` AS `TrmnlMatx`,
`tbl_matx_20160321`.`trmnlsvcdays` AS `TrmnlSvcDays`,
`tbl_matx_20160321`.`trmnltranspts` AS `TrmnlTransPts`,
IF(ISNULL(`orig_lookup`.`daysin`+ `dest_lookup`.`daysout`+ `tbl_matx_20160321`.`trmnlsvcdays`), 'No Service',(`orig_lookup`.`daysin`+ `dest_lookup`.`daysout`+ `tbl_matx_20160321`.`trmnlsvcdays`)) AS `TotalSvcDays`
FROM ((`tbl_uscomp_20160321` AS `orig_lookup` LEFT JOIN `tbl_hcas_20160321` AS `orig_hcas` ON `orig_lookup`.`zipcode` = `orig_hcas`.`zipcode` LEFT JOIN `tbl_ssas_20160321` AS `orig_ssas` ON `orig_lookup`.`zipcode` = `orig_ssas`.`zipcode`)
INNER JOIN (`tbl_uscomp_20160321` AS `dest_lookup` LEFT JOIN `tbl_hcas_20160321` AS `dest_hcas` ON `dest_lookup`.`zipcode` = `dest_hcas`.`zipcode` LEFT JOIN `tbl_ssas_20160321` AS `dest_ssas` ON `dest_lookup`.`zipcode` = `dest_ssas`.`zipcode`)
INNER JOIN `tbl_matx_20160321` ON (`orig_lookup`.`xdock` = `tbl_matx_20160321`.`otrmnl` AND `dest_lookup`.`xdock` = `tbl_matx_20160321`.`dtrmnl`))
WHERE `orig_lookup`.`zipcode` = '$ziporig' AND `dest_lookup`.`zipcode` = '$zipdest'",

Related

UPDATE With Multi JOINS In a Single Query

I am trying to JOIN 4 Tables in one query, it compiles okay, but returns or SET no values. Following is The QUERY i have written. A Little help Please
UPDATE tbl_venue_charges AS VC
JOIN tbl_usr_training_master AS TM ON TM.trai_master_id = VC.vnu_chrg_id
JOIN tbl_trainer_participant_expenses AS TPE ON TPE.trpex_id = VC.vnu_chrg_id
JOIN tbl_usr_training_master2 AS UTM ON UTM.trai_master2_id = VC.vnu_chrg_id
JOIN tbl_travelling_expenses AS TEX ON TEX.trv_exp_id = VC.vnu_chrg_id
SET VC.trai_master_id = TM.trai_master_id AND
VC.trpex_id = TPE.trpex_id,
VC.trai_master2_id = UTM.trai_master2_id,
VC.trv_exp_id = TEX.trv_exp_id;

Select from Multiple table, but ignore if not found

Using this query I'm selecting rows from multiple tables. Unfortunately, if a row does not exist in one table, then rows in all tables won't return. It's because I'm using AND operator.
So, I want this query to be modified, where will ignore a table if value is not found, but return rest of the tables where the value is found.
Here's the MySQL query:
foreach ($courseArr as $term) {
$term = trim($term);
if (!empty($term)) {
$courseSectionSql[] = "courseDataApp.course = '$term' AND courseDataApp.section = '$sectionArr[$i]'";
$i++;
}
}
$data = $db->rawQuery("SELECT courseDataApp.*, facultyDatabase.*, books.*
FROM courseDataApp
INNER JOIN facultyDatabase ON facultyDatabase.initial = courseDataApp.faculty
INNER JOIN books ON books.course = courseDataApp.course WHERE ".implode(' OR ', $courseSectionSql));
Here's what is returns:
{
"id":11,
// courseDataApp values
"faculty":"AKH1",
"course":"CSE241",
"section":"7",
.
.
.
.
.
.
.
// facultyDatabasevalues
"initial":"AKH1",
"name":"Ms. Kamal Habi",
"dept":"ECE",
.
.
.
.
.
.
// books values
"books": "Digital Logic Design"
},
So the problem is, when a value from facultyDatabase or books tables not found, rest of the data won't return. I just want it to ignore that, show what's found. Like Union.
As some of the comments point out you are using outdated syntax that INNER JOINs the tables which leads to the result you get. You need to LEFT JOIN the tables. Hence you will have a result even though there are no entries in the LEFT JOINed tables. Something like this should work
SELECT courseDataApp.*, facultyDatabase.*, books.*
FROM courseDataApp
LEFT JOIN facultyDatabase ON facultyDatabase.initial = courseDataApp.faculty
LEFT JOIN books ON books.course = courseDataApp.course
WHERE courseDataApp.course = '$term'
AND courseDataApp.section = '$sectionArr[$i]'
Written like this you would have an equivalent to your query in current syntax that will not return anything if there are no entries in the INNER JOINed tables.
SELECT courseDataApp.*, facultyDatabase.*, books.*
FROM courseDataApp
INNER JOIN facultyDatabase ON facultyDatabase.initial = courseDataApp.faculty
INNER JOIN books ON books.course = courseDataApp.course
WHERE courseDataApp.course = '$term'
AND courseDataApp.section = '$sectionArr[$i]'

Running the exact same SQL query in PMA and WordPress $wpdb return different results

I have this SQL Query:
SELECT *, GROUP_CONCAT(inter_postmeta.meta_value SEPARATOR '|') AS all_data
FROM inter_woocommerce_order_itemmeta
LEFT JOIN inter_woocommerce_order_items ON inter_woocommerce_order_itemmeta.order_item_id = inter_woocommerce_order_items.order_item_id
LEFT JOIN inter_posts ON inter_posts.ID = inter_woocommerce_order_items.order_id
LEFT JOIN inter_postmeta ON (inter_postmeta.post_id = inter_woocommerce_order_items.order_id
AND (inter_postmeta.meta_key = '_shipping_first_name'
OR inter_postmeta.meta_key = '_shipping_last_name'
OR inter_postmeta.meta_key = '_shipping_postcode'
OR inter_postmeta.meta_key = '_shipping_city'
OR inter_postmeta.meta_key = '_shipping_address_1'
OR inter_postmeta.meta_key = '_shipping_address_2'))
WHERE inter_woocommerce_order_itemmeta.meta_key = '_fc_data' AND inter_woocommerce_order_itemmeta.meta_value LIKE '%2017-2-1'
GROUP BY inter_woocommerce_order_itemmeta.order_item_id
If I execute it on PhpMyAdmin it returns what I need:
But when I run the exact same query using $wpdb->get_results, the meta_key and mea_value fields differ.
How can I get the same results using $wpdb? Annd how is it possible that the exact same query return different results?

Return all rows from table one even if table two is empty or partial results found

I am trying to return all the results from table one, AKA ship_skill_tree, while matching up the rows found in table two, AKA character_sheet_skills, even if the rows do not exist in table two.
SELECT c.`level` , t.`skillLevel` AS levelNeeded, i.`typeName`
FROM `ship_skill_tree` t
LEFT JOIN `character_sheet_skills` c ON t.`skillTypeID` = c.`typeID`
LEFT JOIN `invTypes` i ON i.`typeID` = t.`skillTypeID`
WHERE t.`shipTypeID` = 11176 AND c.`character_id` = 1;
Table One Data:
|shipTypeID|shipGroupID|skillTypeID|skillLevel
______________________________________________
|11011|26|3332|1
|11129|31|3327|1
|11132|31|3327|1
|11134|31|3327|1
|11172|830|3328|5
|11172|830|12093|1
|11174|893|3328|5
|11174|893|28615|1
|11176|831|3330|5
|11176|831|12092|1
Table Two Data:
|character_id|typeID|skillpoints|level|published
______________________________________________
|1|3300|1415|2|1
|1|3301|8000|3|1
|1|3327|256000|5|1
|1|3330|2829|2|1
|1|3340|181020|4|1
|1|3341|1024000|5|1
|1|3342|32000|3|1
|1|3343|32202|3|1
|1|3380|256000|5|1
|1|3385|256000|5|1
|1|3386|256000|5|1
|1|3392|256000|5|1
|1|3394|90514|4|1
|1|3402|256000|5|1
|1|3410|768000|5|1
|1|3411|135765|4|1
|1|3412|750|1|1
|1|3413|256000|5|1
|1|3416|45255|4|1
|1|3417|0|0|1
|1|3418|0|0|1
|1|3419|135765|4|1
|1|3420|181020|4|1
|1|3423|0|0|1
|1|3425|90510|4|1
|1|3426|45255|4|1
|1|3428|500|1|1
|1|3429|8000|3|1
|1|3436|45255|4|1
|1|3437|45255|4|1
|1|3438|500|1|1
|1|3449|256000|5|1
|1|3453|0|0|1
|1|3455|256000|5|1
|1|3456|226275|4|1
|1|11579|271530|4|1
|1|12186|0|0|1
|1|12187|0|0|1
|1|12188|0|0|1
|1|12190|22547|3|1
|1|12191|45255|4|1
|1|12192|45255|4|1
|1|12193|45255|4|1
|1|12195|45255|4|1
|1|16281|256000|5|1
|1|17940|1024000|5|1
|1|20342|1280000|5|1
|1|22551|40000|3|1
|1|22578|181020|4|1
|1|25739|0|0|1
|1|26252|16000|3|1
|1|26253|750|1|1
|1|26261|750|1|1
|1|32918|16000|3|1
invTypes table:
|typeID|typeName
________________
|3327|Spaceship Command
|3328|Gallente Frigate
|3330|Caldari Frigate
|3332|Gallente Cruiser
|12092|Interceptors
|12093|Covert Ops
|28615|Electronic Attack Ships
In the above query shipTypeID will always, or should always, be valid and match a record in table one, however, in table two, the rows that match may not exist. What I need is to output as follows:
|level|levelNeeded|typeName
___________________________
|2|5|Caldari Frigate
|NULL|1|Interceptors
Currently this is what is returned:
|level|levelNeeded|typeName
___________________________
|2|5|Caldari Frigate
EDIT: Solution!
SELECT c.`level` , t.`skillLevel` AS levelNeeded, i.`typeName`
FROM `ship_skill_tree` t
LEFT JOIN `character_sheet_skills` c ON t.`skillTypeID` = c.`typeID` AND c.`character_id` = 1
INNER JOIN `invTypes` i ON i.`typeID` = t.`skillTypeID`
WHERE t.`shipTypeID` = 11176
You need to put any restrictions on the table being joined in the ON clause. If you put them in the WHERE clause it doesn't work, because the rows that don't have any matches will produce NULL for those columns, and the WHERE clause will filter them out.
SELECT c.`level` , t.`skillLevel` AS levelNeeded, i.`typeName`
FROM `ship_skill_tree` t
LEFT JOIN `character_sheet_skills` c ON t.`skillTypeID` = c.`typeID` AND c.`character_id` = 1
LEFT JOIN `invTypes` i ON i.`typeID` = t.`skillTypeID`
WHERE t.`shipTypeID` = 11176
DEMO
You need to use a right join or an outer join rather than a left join. Have a look through the Visual Representation of SQL Joins for a good overview

Mysql database query

I want to fetch data from mysql in three 3 tables. I am using LEFT OUTER JOIN. This is my query:
SELECT cms_addresses.address1 as add1,
cms_addresses.email as ademail,
cms_addresses.city as adcity,
cms_addresses.clientid
cms_addresses.country as adcountry,
cms_addresses.province as adprovince,
cms_addresses.postal as adprovince,
cms_delivery_info.d_address1,
cms_delivery_info.d_city,
cms_delivery_info.d_country,
cms_delivery_info.d_province,
cms_delivery_info.d_postal,
cms_orders.id,
cms_orders.cdate,
cms_orders.message
FROM cms_addresses
LEFT OUTER JOIN cms_delivery_info
ON cms_addresses.clientid = cms_delivery_info.d_clientid
LEFT OUTER JOIN cms_orders = cms_addresses.clientid = cms_orders.clientid
WHERE cms_orders.id = 10
and cms_addresses.addresstypeid = 1
Problem is that this query is not running and gives me that error.
Not unique table/alias: 'cms_addresses'
Please help me out to resolve this. thanks
You had a syntactical error in the second left join, where there was '=' after the table name
TRy this::
SELECT
cms_addresses.address1 as add1,
cms_addresses.email as ademail,
cms_addresses.city as adcity,
cms_addresses.clientid,
cms_addresses.country as adcountry,
cms_addresses.province as adprovince,
cms_addresses.postal as adprovince,
cms_delivery_info.d_address1,
cms_delivery_info.d_city,
cms_delivery_info.d_country,
cms_delivery_info.d_province,
cms_delivery_info.d_postal,
cms_orders.id,
cms_orders.cdate,
cms_orders.message
FROM cms_addresses
LEFT OUTER JOIN cms_delivery_info ON cms_addresses.clientid = cms_delivery_info.d_clientid
LEFT OUTER JOIN cms_orders on cms_addresses.clientid = cms_orders.clientid
WHERE cms_orders.id = 10 and cms_addresses.addresstypeid = 1
put a comma between cms_addresses.clientid and cms_addresses.country AS adcountry and see if it works
SELECT cms_addresses.address1 AS add1,
cms_addresses.email AS ademail,
cms_addresses.city AS adcity,
cms_addresses.clientid, // add comma here
cms_addresses.country AS adcountry,
cms_addresses.province AS adprovince,
cms_addresses.postal AS adprovince,
cms_delivery_info.d_address1,
cms_delivery_info.d_city,
cms_delivery_info.d_country,
cms_delivery_info.d_province,
cms_delivery_info.d_postal,
cms_orders.id,
cms_orders.cdate,
cms_orders.message
FROM cms_addresses
LEFT JOIN cms_delivery_info
ON cms_addresses.clientid = cms_delivery_info.d_clientid
LEFT JOIN cms_orders
// use ON instead of = (equal sign) here
ON cms_addresses.clientid = cms_orders.clientid
WHERE cms_orders.id = 10 AND cms_addresses.addresstypeid = 1
In your SELECT you have at some point:
cms_addresses.clientid cms_addresses.country as adcountry,
which looks like you missed a , somewhere?
In your FROM you have:
LEFT OUTER JOIN cms_orders = cms_addresses.clientid = cms_orders.clientid
which again I'm guessing is a typo (first = should be a ON).
The message you are getting means you are reusing the table cms_addresses in your JOIN chain and you are not giving it an alias, which is mandatory for disambiguation, but in your case it's weird because you don't have cms_addresses more than once in the FROM clause.
I'd try rewriting the query giving aliases to all tables. Also enable full logging and check what really reaches Mysql.

Categories