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

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

Related

how ignore duplicate values without consider of their position in mysql?

I have this table with one column
A:
16654,16661
16661,16654
16670,16717
16717,16670
I want to have this: (ignore duplicate values without consider of their position)
16661,16654
16670,16717
is there any math function that operate between two number and have unique result?
actually i have this table ( name:class)
id second_code have_second_code
1 0 no
2 3 yes
3 2 yes
4 5 yes
5 4 yes
when "have_second_code" is "yes"
column second_code have a value!
id is primary
second code is from id column and there is a binary relation between them. now i need this output 2,3 and 4,5
SELECT rowone, rowtwo, rowonemillion FROM yourtable GROUP BY(nodupecolumn)
I suppose, that your query that produces this one-column-multiple-values-table uses GROUP_CONCAT(). In this case you need to do it like this:
SELECT DISTINCT GROUP_CONCAT(DISTINCT whatever_column ORDER BY whatever_column) FROM ...
Use the DISTINCT keyword two times. In GROUP_CONCAT(), so that duplicates are removed from the comma separated values, and one time outside of GROUP_CONCAT(), so that duplicate rows are removed. The ORDER BY in GROUP_CONCAT() is important, otherwise the outer DISTINCT won't detect duplicates. Also note, that (outer) DISTINCT works on the whole row, not just one column.

Mysql intersect two strings

I have the following tables:
TableFinal
column id, with first row having value 1
column numbers, with first row having value `1,5,6,33,2,12,3,4,9,13,26,41,59,61,10,7,28`
And
TablePick
column id, with first row having value 1
column numbers, with first row having value 2,12,26,33
I want to check if the numbers from TablePick, column "selected" are contained in the column "numbers" of TableFinal.
I have to mention that in TablePick, the numbers in column "selected" are ordered ASC, while in TableFinal, the numbers in column "numbers" are shuffled.
Usually I would put each of these in an array using PHP and then intersect the 2 arrays and count the resulted array. But in MYSQL, it is not that simple, so practically I have no idea where to start.
Maybe I should create an ARRAY_INTERSECT function? Or do we have a simpler solution?
SELECT * FROM TablePick p RIGHT JOIN TableFinal f ON f.id=p.id WHERE ARRAY_INTERSECT(p.selected,f.numbers)
Sorry to say so, but your schema needs some serious maintenance: NEVER EVER store more than one information in one field, if you need to access them separately.
You need a pair of join tables, where instead of the first row (1, "1,5,6,33,2,12,3,4,9,13,26,41,59,61,10,7,28") you have the rows
(1,1)
(1,5)
(1,6)
(1,33)
...
and instead of the row (1, "2,12,26,33") you have the rows
(1,2)
(1,12)
(1,26)
(1,33)
Now you query is simply:
SELECT ... FROM TableFinal
INNER JOIN TABLE TablePick ON TableFinal.number=TablePick.number
WHERE TableFinal.id=1
AND TablePick.id=1
EDIT
Please understand, that even if this were possible without MySQL abuse, it would be a performance killer, once the number of rows start to rise: We are talking of n*m array intersects, if the tables have n and m rows respectivly.

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 query Sum with Case

I have two tables.
One contains default data for 3 columns, (value is 1 or 0) In the same table is a ClientID
Another table contains edited data with a date, ClientID and a extra column named 'Changed'
If Changed = 1 then the values in the 3 columns are changed and therefore need to be read from the second table.
This al works fine, but I want to make a report in php where a daterange can be selected and a query should group by ClientID and count all 1's in the selected daterange of the 3 columns. (Each column seperate)
Here's the trick: When Changed = 0 in the specific row then It should check the default value and if Changed = 1 it should check the second table. And then count it with the previous rows.
I hope you understand what I want to create
You can use IF function from SQL
For example
SELECT SUM(IF(Changed=1, t1.col1, t2.col2)) FROM t1, t2 WHERE t1.id=t2.id
This is example how you can use columns for case in SUM

php mysql compare two columns return mismatches

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.

Categories