Mysql query Sum with Case - php

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

Related

Summing up values from a single table column using PHP

I would like you know how I can sum up all values from a single table column based on a specific constraint. What I mean is this, imagine a table called payments, the payments table has three columns; ID, type of payment and amount. ID is not a primary key so it can duplicate. So let's say there are four entries under one particular ID and I want to get the total for amount for that ID. How can I do it? Thought a while loop would work but tried everything I could think of and it didn't. Please help! Final school project
If you are using mysql you can use the sum function as follows:
SELECT SUM(t1.ID) FROM db1.tblmoney AS t1 WHERE t1.ID = '1';
This will select the sum total of column "ID" in all rows within db1.tblmoney with the ID of 1. If on the other hand you are trying to get a count of the rows with that ID you can use this:
SELECT COUNT(t1.*) FROM db1.tblmoney AS t1 WHERE t1.ID = '1';
The different in these two statements is SUM with get the added values of all ID
So if you have 2 rows of id ='1' you will get 2 if you have two rows of id = '2' you will get 4
Whereas with Count() it will count the number of rows matching your select: 2 rows of id ='1' you will get 2 if you have two rows of id = '2' you will get 2.

How to increment a MySQL database row value

I have a MySQL database with a table called chapters.
I have a column called chapter_number which is an Integer data type and is NOT auto increment as I have another field which uniquely identifies the row.
Upon insertion to this table I would like the value of the chapter_number column to be the previous row's chapter_number incremented by 1 (+1).
The purpose of this is to ensure that every time a chapter is added, the right ordering takes place.
How could I achieve this in PHP?
Here is an example of what I mean?
Table structure:
$stmt = $db->prepare('INSERT INTO chapters VALUES (?,?,?,?)');
$stmt->execute(array('','title', '2015-04-03', 'chapter 1 body'));
What would I put for the first parameter ^
SELECT max(chapter_number) + 1 as total FROM table;
This would also allow you to later on have chapters assigned to multiple books by symply adding a where clause
The following example is how this syntax would look
insert into table_one (greeting_column, name_column)
SELECT
'hello',
(SELECT max(chapter_number) + 1 as total FROM table)

How to use colum name as a value and filter highest value MYSQL/PHP

This is my SqlFiddle I have managed to get all values i need except i havent figured out how to check which value from table "tickets" has highest value as column name in table "draws" and then assign value from table "coefficients".
So, if table tickets have values: 9,12,1,11,14,10
And table "draws" have values: 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,22,21,23,24,25,26,27,28,29,30,31,32,33,1,35
Highest value column name from this "ticket" in table draws is 4th value in table tickets (number 1) and because that is 34th value in table draws i should get 34th value from table "coefficients"
Some PHP or even vb.net function would be ok because i need this for a simple application and it does not matter how it's done as long as it is correct.
Oh, yes... i forgot to put my sql here:
SELECT *,
c.coefficient
FROM tickets t
INNER JOIN draws d ON
(FIELD(t.b1,d.1,d.2,d.3,d.4,d.5,d.6,d.7,d.8,d.9,d.10,d.11,d.12,d.13,d.14,d.15,d.16,d.17,d.18,d.19,d.20,d.21,d.22,d.23,d.24,d.25,d.26,d.27,d.28,d.29,d.30,d.31,d.32,d.33,d.34,d.35)>0)
AND (FIELD(t.b2,d.1,d.2,d.3,d.4,d.5,d.6,d.7,d.8,d.9,d.10,d.11,d.12,d.13,d.14,d.15,d.16,d.17,d.18,d.19,d.20,d.21,d.22,d.23,d.24,d.25,d.26,d.27,d.28,d.29,d.30,d.31,d.32,d.33,d.34,d.35)>0)
AND (FIELD(t.b3,d.1,d.2,d.3,d.4,d.5,d.6,d.7,d.8,d.9,d.10,d.11,d.12,d.13,d.14,d.15,d.16,d.17,d.18,d.19,d.20,d.21,d.22,d.23,d.24,d.25,d.26,d.27,d.28,d.29,d.30,d.31,d.32,d.33,d.34,d.35)>0)
AND (FIELD(t.b4,d.1,d.2,d.3,d.4,d.5,d.6,d.7,d.8,d.9,d.10,d.11,d.12,d.13,d.14,d.15,d.16,d.17,d.18,d.19,d.20,d.21,d.22,d.23,d.24,d.25,d.26,d.27,d.28,d.29,d.30,d.31,d.32,d.33,d.34,d.35)>0)
AND (FIELD(t.b5,d.1,d.2,d.3,d.4,d.5,d.6,d.7,d.8,d.9,d.10,d.11,d.12,d.13,d.14,d.15,d.16,d.17,d.18,d.19,d.20,d.21,d.22,d.23,d.24,d.25,d.26,d.27,d.28,d.29,d.30,d.31,d.32,d.33,d.34,d.35)>0)
AND (FIELD(t.b6,d.1,d.2,d.3,d.4,d.5,d.6,d.7,d.8,d.9,d.10,d.11,d.12,d.13,d.14,d.15,d.16,d.17,d.18,d.19,d.20,d.21,d.22,d.23,d.24,d.25,d.26,d.27,d.28,d.29,d.30,d.31,d.32,d.33,d.34,d.35)>0)
INNER JOIN coefficients c
ON c.number = FIELD(t.b6,d.1,d.2,d.3,d.4,d.5,d.6,d.7,d.8,d.9,d.10,d.11,d.12,d.13,d.14,d.15,d.16,d.17,d.18,d.19,d.20,d.21,d.22,d.23,d.24,d.25,d.26,d.27,d.28,d.29,d.30,d.31,d.32,d.33,d.34,d.35)
WHERE draw='1' AND status = '1'
Any help would be awesome.
I think you can use GREATEST() function
try this sqlFiddle

SQL query using replace a value in one table by randomly selecting a value in another table

I have a table with an value that I would like to be updated periodically with a cron job. However, I need to update the value by replacing it with a value from a different table. The issue is that I would like the replacement value to be chosen randomly.
For example, Table 1 has
ID Email
=================
1 bobatumail
Table 2 has:
ID Email
================
1 bobatumail
2 joeatumail
3 peteatumail
4 biffatumail
5 wilneratumail
6 wilsonatumail
I would like the query to replace bobatumail in Table 1 with any of the other values in Table 2 as long as it is random. It could even be the same value as in Table 1.
Any idea how to do this?
In MySQL you could use the REPLACE statement:
REPLACE INTO table1 (ID, Email)
SELECT 1, table2.Email FROM table2 ORDER BY RAND() LIMIT 1;
The "1" in the second line represents the id of the entry while the second part returns a random value out of table2. Yes, there are solutions using the UPDATE statement (JOIN and ANSI) but its always tricky and you usually have to turn off safe update mode.
http://dev.mysql.com/doc/refman/5.5/en/mysql-command-options.html#option_mysql_safe-updates
Please note that REPLACE first deletes the old entry and then reinserts the new one.
http://dev.mysql.com/doc/refman/5.5/en/replace.html

How do I find which of two tables has the row with a given value in a known column

If I have two tables in a MySQL database that both have a column called order_number, given an order_number value but not knowing which table it comes from how would I go about setting up a query that would return the name of the table it was found in?
I am particularly interested in the name of the table so I can set up subsequent updates to that table.
Also, I am using PHP for the handling of the query.
select "tableA" as tableName,order_number from tableA where order_number=5
UNION
select "tableB" as tableName,order_number from tableB where order_number=5;

Categories