mysql query into multiple columns - mysql

I have a table with 100 columns.
It happens that I want the query to show only the columns that have "value <> 0"
So the column that has val = 0 should not be displayed.
Can anyone help with this

You can have a query like the following:
SELECT FLOOR(SUM(IF(column1 IN (0),0,1))/COUNT(*)),FLOOR(SUM(IF(column2 IN (0),0,1))/COUNT(*)),...,FLOOR(SUM(IF(column100 IN (0),0,1))/COUNT(*)) FROM table_name
This will give you a row with column value = 0 if the column contains 0 and 1 if column contains no 0.

You cannot adjust the number of columns returned. That is defined by a projection (your SELECT block) and a projection is evaluated against the table schema. It is expected that you know the number of columns and the types that you want returned in a projection.
As the previous user had mentioned, you can use a function or case statement to change the value in the column, or alternatively, you can restrict the rows returned with the WHERE clause (if some value or values are <> 0, for example).
The schema of the generated projection table returned must be static though.

Related

MySQL query to match exact value or next lower value

A PHP script returns a value which must be looked up in the first column of a table in a MySQL database.
If the exact value returned by the script appears in that first table column, then the query needs to extract the further information from the other columns along that row.
If the script value doesn't appear in the first table column, then the same information should be extracted from the next lowest value, should it exist.
So the database has rows looking like this, with the target lookup value in the first column:
18 0.3783 0.4438 0.5155 0.5614 0.6787
19 0.3687 0.4329 0.5034 0.5487 0.6652
20 0.3598 0.4227 0.4921 0.5368 0.6524
25 0.3233 0.3809 0.4451 0.4869 0.5974
30 0.2960 0.3494 0.4093 0.4487 0.5541
If the returned script value is 25, then the five other other values in the same row:
0.3233 0.3809 0.4451 0.4869 0.5974
...should be extracted.
If the script value however is 24, then the row information with the next lowest value needs to be extracted, which would be that for the database first-column value 20, being:
0.3598 0.4227 0.4921 0.5368 0.6524
What sort of query would be best suited for this?
You can filter on rows whose first column is equal to or less than the parameter, order by descending value in that column, and keep the top row only, using limit.
Assuming that the filtering column is called id:
select *
from mytable
where id <= ?
order by id desc
limit 1
The question mark represents the parameter to the query.

Why does SELECT COUNT(*) return 1 when no table is selected? [duplicate]

could you please explain why mysql count function without providing any table name gives 1 as value?
SELECT COUNT(*);
Result: 1
Because in mysql select constant_value command is valid (such as select 2 will return 2) and will return 1 row. Count() function without group by will collapse the resultset and count the number of items in the resultset. In this case 1 row would be returned and count(*) counts that.
Normally all selects are of the form SELECT [columns, scalar computations on columns, grouped computations on columns, or scalar computations] FROM [table or joins of tables, etc]
Because this allows plain scalar computations we can do something like SELECT 1 + 1 FROM SomeTable and it will return a recordset with the value 2 for every row in the table SomeTable.
Now, if we didn't care about any table, but just wanted to do our scalar computed we might want to do something like SELECT 1 + 1. This isn't allowed by the standard, but it is useful and most databases allow it (Oracle doesn't unless it's changed recently, at least it used to not).
Hence such bare SELECTs are treated as if they had a from clause which specified a table with one row and no column (impossible of course, but it does the trick). Hence SELECT 1 + 1 becomes SELECT 1 + 1 FROM ImaginaryTableWithOneRow which returns a single row with a single column with the value 2.
Mostly we don't think about this, we just get used to the fact that bare SELECTs give results and don't even think about the fact that there must be some one-row thing selected to return one row.
In doing SELECT COUNT() you did the equivalent of SELECT COUNT() FROM ImaginaryTableWithOneRow which of course returns 1.
Reference

Why MySQL COUNT without table name gives 1

could you please explain why mysql count function without providing any table name gives 1 as value?
SELECT COUNT(*);
Result: 1
Because in mysql select constant_value command is valid (such as select 2 will return 2) and will return 1 row. Count() function without group by will collapse the resultset and count the number of items in the resultset. In this case 1 row would be returned and count(*) counts that.
Normally all selects are of the form SELECT [columns, scalar computations on columns, grouped computations on columns, or scalar computations] FROM [table or joins of tables, etc]
Because this allows plain scalar computations we can do something like SELECT 1 + 1 FROM SomeTable and it will return a recordset with the value 2 for every row in the table SomeTable.
Now, if we didn't care about any table, but just wanted to do our scalar computed we might want to do something like SELECT 1 + 1. This isn't allowed by the standard, but it is useful and most databases allow it (Oracle doesn't unless it's changed recently, at least it used to not).
Hence such bare SELECTs are treated as if they had a from clause which specified a table with one row and no column (impossible of course, but it does the trick). Hence SELECT 1 + 1 becomes SELECT 1 + 1 FROM ImaginaryTableWithOneRow which returns a single row with a single column with the value 2.
Mostly we don't think about this, we just get used to the fact that bare SELECTs give results and don't even think about the fact that there must be some one-row thing selected to return one row.
In doing SELECT COUNT() you did the equivalent of SELECT COUNT() FROM ImaginaryTableWithOneRow which of course returns 1.
Reference

expression to count field only if another field is distinct in the dataset

I have a column in my report that adds up the value of a field depending on that fields value, like this:
=COUNT(iif(Fields!isAcceptable.Value > 0, 1, Nothing))
However, I need that count to be dependent on the another field called testItemId. Sometimes testItemId, can appear 2 or more times in the dataset, but I need the above count expression to only count once per testItemId. So if the same testItemId appears 4 times, I only want the isAceptable value to be counted once.
I also tried COUNTDISTINCT, but that just gave me a '-1' for each row.
Is there a way to do do this in the expression?
Thanks
Try this:
=CountDistinct(IIF(Fields!isAcceptable.Value>0,Fields!TestID.Value,Nothing))
It will produce this:
UPDATE:
It says: if the row is acceptable (greater than 0) it will pass TestID to the CountDistinct function otherwise it will pass nothing (think the row is ommited). CountDistinct will get the TestID values sum by 1 when a new TestId value is passed.
Returns a count of all distinct non-null values specified by the
expression, evaluated in the context of the given scope.
Reference
Let me know if this helps you.

Trying to avoid duplicate SQL entries

I am trying to avoid adding a certain game's data to my table more than once so I am trying to make an if statement that would check if that game's ID is already in the table, however for some reason the if statement is always false. This is my code:
$a = $_GET['id'];
$colname = $_GET['colname'];
$b = "SELECT count(*)
FROM table
WHERE gameid = ".$a;
if($dup = mysqli_query($dbc, $b)){
if(mysqli_num_rows($dup)==0){
$insrt = "INSERT INTO table ($colname)
VALUES ($a)";
mysqli_query($dbc, $insrt);
}
}
If I were you, instead of using logic within your program to avoid creating duplicate entries, I would simply tell MySQL that your ID column should be unique. Take a look at the info on column definitions in the MySQL Reference Manual, specifically the keywords UNIQUE or PRIMARY KEY.
If you specify that your ID column should be a unique index, then MySQL will prevent another entry with the same ID value from being added. That way, in your program, you can simply attempt to add the data, and the procedure will automatically fail if it is a duplicate. As an added bonus, this means you'll only have to do one query from your program instead of two.
A SELECT COUNT().... query, barring exceptional circumstances, is generally going to return at least one row (more if there is a GROUP BY clause that would indicate otherwise); you need to check that the field value is 0, not that there are no rows in the result.
Change your query to remove the aggregate, and just return a column, e.g.
SELECT gameid
FROM table
WHERE gameid = ?
LIMIT 1
You don't need a count of rows, you just need to know whether a row is returned.
Or, you could add a HAVING clause to your query to not return a row when the COUNT is zero...
SELECT COUNT(*)
FROM table
WHERE gameid = ?
HAVING COUNT(*) > 0
There's no need for you to retrieve the value of the column from the row that's returned, just return an empty resultset, and test whether the resultset is empty, like your code is doing.