2 mysql queries at once - mysql

I have a table like this one..
[id][Col1][Col2][id_duplicated]
10 abc1 defg NULL
12 text text NULL
50 abc2 text NULL
90 NULL NULL 10
500 NULL NULL 10
620 NULL NULL 50
700 text text NULL
Id_duplicated is a value that marks that is a copy from the row 'id' on the same table.. if i select id 620 will display all values from id 50
The problem:
select id,col1 from table where col1 is like '%abc%'
Only will show the row id=10 and id=50 but also i need to display if there's any copies of those id's
i imagine i need a subquery that first find if there's any '%abc%' and then a second one to check if there's any copy on id_duplicated equal to 10 and 50 and etc etc....
So in conclusion i need one query that display this result
[id][Col1][Col2][id_duplicated]
10 abc1 defg NULL
50 abc2 text NULL
90 NULL NULL 10
500 NULL NULL 10
620 NULL NULL 50
Sorry my english

If you want additional rows you can use union and a select looking for the id of the result into the id_duplicated:
http://sqlfiddle.com/#!9/ad817/8
select id,col1, col2, id_duplicated
from table
where col1 like '%abc%'
UNION
select id, col1, col2, id_duplicated
FROM table
WHERE id_duplicated IN (select id from table where col1 like '%abc%')

Related

displaying rows with not null columns excluded

I have a MySQL table named products with default NULL value for size, weight, dimensions
I need to fetch it to get rows with non NULL values the desired result would be like for first row
:
SKU name price size
1 ,product1 ,2.00 , 2
and the second row
SKU name price weight
2 ,product2 ,3.00 , 3
I tried COALESCE but I need also to get the corresponding column name for the non NULL value
SKU name price size weight dimensions
1 product1 2.00 2 NULL NULL
2 product2 3.00 NULL 3 NULL
3 product3 4.00 NULL NULL 2x11x22
Use CONCAT() to combine the column name with the column value. If the value is NULL, the concatenation will also be NULL, and CONCAT_WS() will ignore that argument.
CONCAT_WS(', ',
CONCAT('size = ', size),
CONCAT('weight = ', weight),
CONCAT('dimensions = ', dimensions)
) AS attributes
CONCAT_WS() will ignore any null values in the arguments, so you won't get ,, in the result.
A solution using COALESCE.
COALESCE returns the first non-NULL value in the list, or NULL if there are no non-NULL values.
SELECT
SKU,
name,
price,
COALESCE(size,weight,dimensions) as extraInfo
FROM mytable;
see: DBFIDDLE
output:
SKU
name
price
extraInfo
1
product1
2.00
2
2
product2
3.00
3
3
product3
4.00
2x11x22

How to exclude ID if there are two duplicate results of id and COD_A combination, select the one that is not NULL?

I have this table in MySQL 5.6.36-82.0:
id
COD_A
COD_B
...
...
...
205
6
NULL
205
6
2
442
2
7
442
7
NULL
...
...
...
Expected output:
id
COD_A
COD_B
...
...
...
205
6
2
442
2
7
442
7
NULL
...
...
...
In this example I expect to return a select without the row | 205 | 6 | NULL |
We can use group by id,COD_A having count(*) >1 to get those rows with duplicate id and COD_A combination. Meanwhile, those lines must have COD_B is null. Finally, exclude the rows matching the above conditions using NOT EXISTS to get the desired rows from the main table:
select * from tb t where not exists
(
select id,COD_A from tb where id=t.id and COD_A=t.COD_A group by id,COD_A having count(*) >1
and
COD_B is null
);
select id, cod_a, max(cod_b) COD_B from t group by cod_a order by id asc
Result :
SELECT * FROM example WHERE id = 205 AND Col_b <> "NULL" OR id <> 205;
This line returns the table you want, there might be a better way to do it but it works.
In general it's not a good idea to have a repeated value in your "id" column since it's your identifier after all.
You can do:
select * from t where id <> 205 or cod_b is not null
Incidentally, MySQL 5 is reaching end of life next year. Maybe it's worth considering an upgrade.

MySQL - How to return distinct IDs where all rows for the same ID have null field value

I have a query with two joins that returns this data:
ID Score
1 NULL
1 5
1 6
2 NULL
2 NULL
3 5
3 8
3 3
3 NULL
3 NULL
3 7
4 NULL
4 NULL
4 3
4 9
I would like to return the unique IDs which have a NULL value in the Score column for each of the rows with the same ID. In this case, the query should only return one row with the ID of 2 since that is the only ID which has all NULL values in the Score column.
Thank you!
You could aggregate your original query by making it as a sub select and aplly count() on score column which has null values, So if all values are NULL for a particular ID then count will return 0, thus using having clause you can filter your results using result of count(Score)
select ID,
count(Score) count_null
from (your query)t
group by ID
having count_null = 0
Demo Based on your sample data set

returning column names in a query in sql

I have a query that looks something like this:
select *
from resident
where Resident_Sex is null or Resident_Date_Of_Birth is null or race_code is null.
not always is it only checking if 3 columns are null, it may have more.
I want to know if there is a way, instead of selecting * from this table, to select lets say the resident id and the column name that he is missing.
for example, it should return
resident_id column_name
----------- ------------
aaaaaaa resident_sex
bbbbbbb resident_sex
bbbbbbb race_code
ccccccc resident_date_of_birth
instead of returning
resident_id resident_sex race_code Resident_date_of_birth
----------- ------------ ----------- -----------------------
aaaaaaa null 158 1995-02-18 00:00:00.000
bbbbbbb null null 1928-07-15 00:00:00.000
ccccccc F 12 null
Obviously there are alot more rows and columns in the table so I cannot do any case or if statements...
I hope I made myself clear...
thank you in advance!
You can unpivot your table using a series of UNION ALL operations. Then simply select every row that contains a NULL-valued column:
SELECT resident_id, column_name
FROM (
SELECT resident_id, 'resident_sex' AS column_name
FROM mytable
WHERE resident_sex IS NULL
UNION ALL
SELECT resident_id, 'race_code'
FROM mytable
WHERE race_code IS NULL
UNION ALL
SELECT resident_id, 'Resident_date_of_birth'
FROM mytable
WHERE Resident_date_of_birth IS NULL) AS t

Join Distinct Id on non-distinct id (MySql)

I'm trying to join distinct ID's from a subquery in a FROM onto a table which has the same ID's, but non-distinct as they are repeated to create a whole entity. How can one do this? All of my tries are continuously amounting to single ID's in the non-distinct-id-table.
For example:
Table 1
ID val_string val_int val_datetime
1 null 3435 null
1 bla null null
1 null null 2013-08-27
2 null 428 null
2 blob null null
2 null null 2013-08-30
etc. etc. etc.
Virtual "v_table" from SubQuery
ID
1
2
Now, if I create the query along the lines of:
SELECT t.ID, t.val_string, t.val_int, t.val_datetime
FROM table1 AS t
JOIN (subquery) AS v_table
ON t.ID = v_table.ID
I get the result:
Result Table:
ID val_string val_int val_datetime
1 null 3436 null
2 null 428 null
What I'd like is to see the whole of Table 1 based on this example. (Actual query has some more parameters, but this is the issue I'm stuck on).
How would I go about making sure that I get everything from Table 1 where the ID's match the ID's from a virtual table?
SELECT t.ID, t.val_string, t.val_int, t.val_datetime
FROM table1 AS t
LEFT JOIN (subquery) AS v_table
ON t.ID = v_table.ID
Sample fiddle