php mysql compare two columns return mismatches - php

I have two columns in different product tables.
tblproduct1.partno is an old product list
tblproduct2.partno2 is a new one
Both partno columns should have identical model numbers but they don't.
When executing the below query, I get about 300 model numbers that don't match when comparing counts from both tables. tblproduct2 has 1955 records, the query below is 1638. I would expect it to return 1955.
SELECT COUNT(partno)
FROM tblproduct1
INNER JOIN tblproduct2 ON partno = partno2
Is there a way I can list the model numbers that don't match?

select tblproduct1.partno from tblproduct1
left join tblproduct2 on tblproduct1.partno = tblproduct2.partno2
where tblproduct2.partno2 is null
shows tblproduct1.partno that have no matching tblproduct2.partno2 values

Actually stereofrogs query is correct. It works even when the table columns are defined as 'not null' I suspect you had the two tables mixed up when you ran the query.
that is because the LEFT JOIN always has all the rows from the left table in it. If the second table does not have a matching entry it will be displayed as NULL.
So as long as you have the table with more rows as the left (or the first) table the above query will produce the desired result.

Related

Mysql Join 2 Table and merge 2 Column, But remove Duplicate

I have 2 Table from where I want customerid, customername, comment and customercontactno.
I use Following query For Join 2 Table.
SELECT comment.id, comment.Kommentar, comment.Kunde,
CONCAT_WS('', customer.telefonPrivat, customer.TelefonMobil) AS Contact_Phone
FROM tbl_test_comment comment
LEFT JOIN tbl_test_customer customer
ON customer.id = comment.Kunde;
My First table is tbl_test_comment With following data
And tbl_test_customer
Result Of Above Query
ISSUE
When I run above query, Its working fine if one of two merged column is empty. But it merge data if data are in both row. I want to avoid one if both row have value.
Expected Output
concat_ws stands for "concatenate with separator", that is, add the strings together with the separator in between.
Instead, use the coalesce function, which returns the first non-null argument:
coalesce(customer.telefonPrivat, customer.TelefonMobil)
If an empty telephone number can be an empty string '' as well as null, you can use the more powerful case statement:
case
when length(customer.telefonPrivat) > 0 then customer.telefonPrivat
else customer.TelefonMobil
end

Need help in a sql query

I have a field that is a varchar that contain values such as (88,90,100,200) and i have another one contains the value 200 when using the IN clause to see if the second field in the first field it returns empty result but when comparing it with 88 it return results
So i was wondering what im doing wrong here and if there is a better way.
Here is the mysql code
select * from user inner join category where parent_id IN (categories)
parent_id is located in the category table and the categories in the user table
When you do a JOIN you need an ON clause to specify that you are looking for rows that somehow "match" across two tables. If you want all the rows from a single table where some column has a value that is "one of the values in this list..." you use IN. So these are both valid SQL:
SELECT * FROM user INNER JOIN category ON user.someColumn = category.someOtherColumn // can add WHERE clause
or
SELECT * FROM user WHERE parent_id IN (88, 99, 100, 200)
Can't tell you exactly what query you should use unless you share your table structures, as the question is unclear.

How to remove empty row of NULL fields from mysql query containing sum

I have this following query which join multiple tables
SELECT targa,
registrazioni.turno as turno,
conduttori.nome as nome,
conduttori.cognome as cognome,
spese_importo,risparmio,km, SUM(spese.spese_importo) AS totspese
FROM registrazioni LEFT JOIN conduttori
ON id_conduttore=conduttori.id
LEFT JOIN spese
ON registrazioni.id=spese.id_registrazione
WHERE dataora='$data';
when I added this SUM(spese.spese_importo), I started having a small issue, and even if the query shouldn't have for me any matching result, I obtain as result an empty row where every field is NULL.
Of course if I remove that SUM from my query, it does work again and I don't have any rows as result of the query
How could I solve it and check if there are results not null?
I've tried via mysql to add the condition
WHERE targa IS NOT NULL
(targa is just one field I've randomly choosen between the different fields they are all NULL)
but it didn't clear the row, and via php I was using the condition
mysqli_num_rows($result)==0
but now that I always have at least 1 row it doesn't work.
any other ideas for checking it before fetching the rows?
I think you want to group your results.
Try to add this at the end of your query :
GROUP BY registrazioni.id
From http://www.w3schools.com/sql/sql_join_left.asp:
The LEFT JOIN keyword returns all rows from the left table (table1), with the matching rows in the >right table (table2). The result is NULL in the right side when there is no match.
Does including sum(spese.spese_importo) create a situation where you would get a match on the left side of the join but not on the right side - and have those NULLs?

MySQL Join table and get results that don't exist in one

I have two tables, one that has a foreign key from the other. I want to get all records that don't exist in the foreign table, based on certain criteria.
Here are the tables I have:
item_setting
setting_id
category_id
item
item_id
setting_id
name
expired_dt
Here's the query I'm using now:
SELECT
iset.setting_id
FROM
item_settings iset
LEFT OUTER JOIN
item i ON i.setting_id = iset.setting_id
WHERE
iset.category_id = '5' AND i.setting_id is null
This query works in providing any setting_id's that do not have a record in the item's table within a specific category.
However, now I want to include cases where the expired_dt less than than time() (meaning it's past expired). In otherwords, I would think to add this:
WHERE
iset.category_id = '5' AND (i.setting_id is null OR i.expired_dt < '".time()."')
However, this doesn't work, it returns all the records.
Any suggestions? Maybe I'm completely over complicating this.... I just want to return the setting_id's from the item_settings table, where the expired_dt associated in the item table is expired or if it does not even exist in the item table.
Thank you!
Try moving the timestamp condition into the join clause. Something like
item_settings iset
LEFT OUTER JOIN
item i ON i.setting_id = iset.setting_id and i.expired_dt > time()

PHP MYSQL Query Multiple Tables Where the Second Table returns multiple rows

I'm trying to query 2 tables where the first table will return 1 row and the second table will return multiple rows. So basically the first table with return text on a page and the second table will return a list that will go within the page. Both tables have a reference row which is what both tables are queried on. (See Below)
SELECT shop_rigs.*, shop_rigs_images.*, shop_rigs_parts.*
FROM shop_rigs
LEFT JOIN shop_rigs_images
ON shop_rigs.shoprigs_ref = shop_rigs_images.shoprigsimg_ref
LEFT JOIN shop_rigs_parts
ON shop_rigs.shoprigs_ref = shop_rigs_parts.shoprigsparts_ref
WHERE shoprigs_enabled='1' AND shoprigs_ref='$rig_select'
ORDER BY shoprigs_order ASC
Is it better to just do 2 queries?
Thanks,
dane
I would do this in two queries. The problem isn't efficiency or the size of the respective tables, the problem is that you're create a Cartesian product between shop_rigs_images and shop_rigs_parts.
Meaning that if a given row of shop_rigs has three images and four parts, you'll get back 3x4 = 12 rows for that single shop_rig.
So here's how I'd write it:
SELECT ...
FROM shop_rigs
INNER JOIN shop_rigs_images
ON shop_rigs.shoprigs_ref = shop_rigs_images.shoprigsimg_ref
WHERE shoprigs_enabled='1' AND shoprigs_ref='$rig_select'
ORDER BY shoprigs_order ASC
SELECT ...
FROM shop_rigs
INNER JOIN shop_rigs_parts
ON shop_rigs.shoprigs_ref = shop_rigs_parts.shoprigsparts_ref
WHERE shoprigs_enabled='1' AND shoprigs_ref='$rig_select'
ORDER BY shoprigs_order ASC
I left the select-list of columns out, because I agree with #Doug Kress that you should select only the columns you need from a given query, not all columns with *.
If you're pulling a large amount of data from the first table, then it would be better to do two queries.
Also, for efficiency, it would be better to specify each column that you actually need, instead of all columns - that way, less data will be fetched and retrieved.
Joins are usually more efficient than running 2 queries, as long as you are joining on indexes, but then it depends on your data and indexes.
You may want to run a "explain SELECT ....." for both options and compare "possible keys" and "rows" from your results.

Categories