I wonder if it is possible with MySQL. I output the result of my "select" query. The result is always one single row. What I want is to output only columns with values not equal to 0. I do not want the user to see everything because lots of columns in the row are 0 and thus not interesting for him.
Example row:
column name: a b c d e f
column value: 1 6 0 6 7 0
I do not want columns c and f to appear in the result.
Can that be achieved in SQL resp. MySQL? Or do I have to perform additional processing of SQL result with some programming language like PHP?
Best regards
Ewgenij
You can't have dynamic columns with a static query.
The only way to have dynamic columns in MySQL is with a stored procedure (which will have to build a query as a string and run it).
Related
col_1 col_2
0 ab,bc,cd
1 bc,xy
2 zz,xx
3 ab
4 cc
5 ef,kk,ok
I want to select rows that have "ab" as one of the values in col_2. For example - in this case, 0th and 3rd row will be selected.
So, is there any SQL query for that?
First, you should fix your data model. Storing multiple values in a string is just a misuse of strings. The correct data model would have a separate row for each col_1/col_2 combination.
Sometimes, we are stuck with other people's really bad decisions on data modeling. MySQL actually has a function to help deal with this, find_in_set().
You can use:
where find_in_set('ab', col_2) > 0
until you fix the data model.
I am using MySQL through R. I am working with two tables within the same database and I noticed something strange that I can't explain. To be more specific, when I try to make a connection between the tables using a foreign key the result is not what it should be.
One table is called Genotype_microsatellites, the second table is called Records_morpho. They are connected through the foreign key sample_id.
If I only select records with certain characteristics from the Genotype_microsatellites table using the following command...
Gen_msat <- dbGetQuery(mydb, 'SELECT *
FROM Genotype_microsatellites
WHERE CIDK113a >= 0')
...the query returns 546 observations for 52 variables, exactly what I would expect. Now, I want to do a query that adds a little more info to my results, specifically by including data from the Records_morpho table. I, therefore, use the following code:
Gen_msat <- dbGetQuery(mydb, 'SELECT Genotype_microsatellites.*,
Records_morpho.net_mass_g,
Records_morpho.svl_mm
FROM Genotype_microsatellites
INNER JOIN Records_morpho ON Genotype_microsatellites.sample_id = Records_morpho.sample_id
WHERE CIDK113a >= 0')
The problem is that now the output has 890 observation and 54 variables!! Some sample_id values (i.e., the rows or individuals in the data frame ) are showing up multiple times, which shouldn't be the case. I have tried to fix this using SLECT DISTINCT, but the problem wouldn't go away.
Any help would be much appreciated.
Sounds like it is working as intended, that is how joins work. With A JOIN B ON A.x = B.y you get every row from A combined with every row from B that has a y matching the A row's x. If there are 3 rows in B that match one row in A, you will get three result rows for those. The A row's data will be repeated for each B row match.
To go a little further, if x is not unique and y is not unique. And you have two x with the same value, and three y with that value, they will produce six result rows.
As you mentioned DISTINCT does not make this problem go away because DISTINCT operates across the result row. It will only merge result rows if the values in all selected fields are the same on those result rows. Similarly, if you have a query on a single table that has duplicate rows, DISTINCT will merge those rows despite them being separate rows, as they do not have distinct sets of values.
If I have a table like so:
Name Type Val
Mike A 1
John A 4
Jerry 6
(Notice that Jerry has an empty string for Type)
And I do a query like
Select sum(Val), Type from table
How does MySQL choose which Type to put in the one row result? If I wanted to return the "non blanked"
To give some context, the Type for every row in this table should actually be the same, but there used to be a bug where there are now some values that are blank. (Note that the sum should still include the blanks)
I know I can do this in two queries, and just select Type from table where Type!="" but I was curious if there was a trick to do it in that single query.
This is valid SQL in MySQL. You are aggregating all rows to one row (by using the aggregate function SUM and having no GROUP BY clause).
Any value that is neither in a GROUP BY clause nor being aggregated is a random one. The type you get is just one of those types in the table; it could even be another one when you execute the same query again.
You can use max(type) or min(type) for instance to get the value you are after.
I have a table A which looks like:
number value
1 A
1 B
2 C
And I have a csv file which contains number as one of the columns. When I do a (pentaho)database lookup on this table with number from that csv file i get an output like:
number value
1 A
2 C
Is there any other way in ETL where the output must be like:
number value
1 A
1 B
2 C
The Database Value Lookup step is designed to return at most 1 row for any given input value. If you want to get all rows for a key you can use a Database Join step, or read all rows from the table and the csv file, sort them, and flow them through a Merge Join step.
These correspond roughly to a nested lookup join and a sort merge join respectively. You would choose between them in the same way a query optimizer would. Basically the rule of thumb is if the number of rows in the table and the csv are roughly the same, the Merge Join will be faster, otherwise use the Database Join step. This is of course a 'rule of thumb', and will not suit every situation. Experimentation is encouraged if performance is critical.
I came across the following SQL statement and I was wondering if it was valid:
SELECT COUNT(*)
FROM
registration_waitinglist,
registration_registrationprofile
WHERE
registration_registrationprofile.activation_key = "ALREADY_ACTIVATED"
What does the two tables separated by a comma mean?
When you SELECT data from multiple tables you obtain the Cartesian Product of all the tuples from these tables. It can be illustrated in the following way:
This means you get each row from the first table paired with all the rows from the second table. Most of the time, it is not what you want. If you really want it, then it's clearer to use the CROSS JOIN notation:
SELECT * FROM A CROSS JOIN B;
In this context, it means that you are going to be joining every row from registration_waitinglist to every row in registration_registrationprofile
It's called a cartesian join
That query is 'syntactically' correct, meaning it will run. What the query will return is the entire product of every row in registration_waitinglist x registration_registrationprofile.
For example, if there were 2 rows in waitinglist and 3 rows in profile, then 6 rows will be returned.
From a practical matter, this is almost always a logical error and not intended. With rare exception, there should be either join criteria or criteria in the where clause.