MySQL Query idea needed - php

I have a table, with the students of a class:
+----+-----------+----------+---------+
| id | nume | prenume | absente |
+----+-----------+----------+---------+
| 1 | Radu | Catalina | 0 |
| 2 | Maselusa | Andreea | 0 |
| 3 | Goaga | Ramona | 0 |
| 4 | Stoica | Teodor | 0 |
| 5 | Petrache | Adrian | 0 |
| 6 | Stoica | Dragos | 0 |
| 7 | Florea | Valeriu | 0 |
| 8 | Coleasa | Ionut | 0 |
| 9 | Panait | Andreea | 0 |
| 10 | Vasile | Codrut | 0 |
| 11 | Ungureanu | Costin | 0 |
| 12 | Pantazi | Daniel | 0 |
| 13 | Stroe | Stefan | 0 |
| 14 | Cojocaru | Iulian | 0 |
| 15 | Pirvu | David | 0 |
| 16 | Ion | Raluca | 0 |
| 17 | Olaru | Andreea | 0 |
+----+-----------+----------+---------+
with id, last name, first name and absence.
Then, I have another table:
+----+--------------+---------------+
| id | nume_materie | medie_actuala |
+----+--------------+---------------+
| 1 | Limba romana | 0 |
| 2 | Matematica | 0 |
| 3 | Fizica | 0 |
| 4 | Chimie | 0 |
| 5 | Biologie | 0 |
| 6 | Informatica | 0 |
| 7 | Engleza | 0 |
| 8 | Franceza | 0 |
| 9 | Geografie | 0 |
| 10 | Istorie | 0 |
| 11 | Sport | 0 |
| 12 | Economie | 0 |
| 13 | Psihologie | 0 |
+----+--------------+---------------+
With id, name of the class and actual average grade.
Now, I need to print one of those students with all classes and grades.
Example:
I want to print this in PHP:
Radu Catalina, 0 absences.
Grades:
1. Limba Romana: 8
2. Matematica: 9
3. Fizica: 10 etc. etc. etc.
How can I do this? I have no idea.. I am a beginner. I have some idea about foreign keys and something like that, but I don't know how to use it. Please help.

You can use 'natural join'.
//Example
SELECT table1.*, table2.*
FROM table1,table2
Where table1.nume = 'Radu';
You can insert a foreign key either through create a table or through the alter table.
After creating a table you can use alter table.

Related

Query MySQL To Show Counting from the table relation

i have 3 tables. 2 reference table and one table transactions.
Table puskesmas
mysql> select id,nama from puskesmas;
+----+----------------------+
| id | nama |
+----+----------------------+
| 1 | BAMBANGLIPURO |
| 2 | BANGUNTAPAN |
| 3 | BANTUL |
| 4 | DLINGO |
| 5 | IMOGIRI |
| 6 | JETIS |
| 7 | KASIHAN |
| 8 | KRETEK |
| 9 | PAJANGAN |
| 10 | PANDAK |
+----+----------------------+
Table poli
mysql> select * from poli;
+----+---------------+
| id | nama |
+----+---------------+
| 1 | BP |
| 2 | KIA |
| 3 | GIGI |
| 4 | JIWA |
+----+---------------+
Table loket
mysql> select pasien_id,poli_id,puskesmas_id from loket limit 10;
+-----------+---------+--------------+
| pasien_id | poli_id | puskesmas_id |
+-----------+---------+--------------+
| 1 | 1 | 1 |
| 5 | 2 | 2 |
| 1 | 1 | 3 |
| 9 | 3 | 4 |
| 1 | 3 | 5 |
| 5 | 2 | 1 |
| 1 | 1 | 3 |
| 8 | 1 | 2 |
| 10 | 3 | 6 |
| 5 | 2 | 5 |
+-----------+---------+--------------+
I want to show the total number (count) of visits to puskesmas table. then made field all_total
fter that I want to take patient visits where poli_id = 1 and used as a column bp. for other poly are the same as the
+----+----------------------+--------------+------+------+------+------+
| id | nama | All Total | BP | KIA | GIGI | JIWA |
+----+----------------------+--------------+------+------+------+------+
| 1 | BAMBANGLIPURO | 30 | 10 | 3 | 15 | 2 |
| 2 | BANGUNTAPAN | 35 | 20 | 4 | 11 | 0 |
| 3 | BANTUL | 15 | 10 | 0 | 5 | 0 |
| 4 | DLINGO | 12 | 10 | 1 | 1 | 0 |
| 5 | IMOGIRI | 10 | 5 | 2 | 1 | 2 |
| 6 | JETIS | 25 | 13 | 3 | 7 | 2 |
| 7 | KASIHAN | 20 | 10 | 10 | 0 | 0 |
| 8 | KRETEK | 23 | 20 | 1 | 1 | 1 |
| 9 | PAJANGAN | 10 | 5 | 1 | 3 | 1 |
| 10 | PANDAK | 0 | 0 | 0 | 0 | 0 |
+----+----------------------+--------------+------+------+------+------+
I've tried to make a query to display like this but it always fails to hold. Can anyone help to make such queries desired results
i have tried query like this :
select puskesmas_id,
sum(case when poli_id=1 then 1 else 0 end) bp,
sum(case when poli_id=5 then 0 else 1 end) gigi,
count(id) total
from loket
group by puskesmas_id
and result like this :
+--------------+------+------+-------+
| puskesmas_id | bp | gigi | total |
+--------------+------+------+-------+
| 1 | 97 | 126 | 143 |
| 6 | 74 | 185 | 210 |
| 8 | 131 | 179 | 190 |
| 7 | 90 | 92 | 98 |
| 2 | 33 | 39 | 54 |
| 4 | 158 | 248 | 263 |
| 3 | 66 | 68 | 72 |
+--------------+------+------+-------+
but puskesmas_id the value 0 does not perform data when querying

checking query data whether all input or not using MySQL

here's small part of table
+-------------+-------------+----------+----------+------------------------+
| sourceindex | targetindex | source | target | new_count |
+-------------+-------------+----------+----------+------------------------+
| 0 | 0 | this | this | 4.514337716384391e-18 |
| 0 | 1 | this | is | 5.501850344983498e-17 |
| 0 | 2 | this | a | 5.501850344983498e-17 |
| 0 | 3 | this | book | 1.805735523541796e-17 |
| 0 | 4 | this | , | 5.501850344983498e-17 |
| 0 | 5 | this | that | 1.805735523541796e-17 |
| 0 | 6 | this | is | 5.501850344983498e-17 |
| 0 | 7 | this | a | 5.501850344983498e-17 |
| 0 | 8 | this | pen | 1.805735523541796e-17 |
| 0 | 9 | this | . | 5.501850344983498e-17 |
| 0 | 10 | this | 這 | 1.805735523541796e-17 |
| 0 | 11 | this | 是 | 5.501850344983498e-17 |
| 0 | 12 | this | 一 | 5.501850344983498e-17 |
| 0 | 13 | this | 本 | 1.805735523541796e-17 |
| 0 | 14 | this | 書 | 1.805735523541796e-17 |
| 0 | 15 | this | , | 5.501850344983498e-17 |
| 0 | 16 | this | é‚£ | 1.805735523541796e-17 |
| 0 | 17 | this | 是 | 5.501850344983498e-17 |
| 0 | 18 | this | 一 | 5.501850344983498e-17 |
| 0 | 19 | this | æž | 1.805735523541796e-17 |
| 0 | 20 | this | ç­† | 1.805735523541796e-17 |
| 0 | 21 | this | . | 5.501850344983498e-17 |
| 1 | 0 | is | this | 5.501850344983498e-17 |
| 1 | 1 | is | is | 3.780758595799811e-17 |
| 1 | 2 | is | a | 1.5123035298142912e-16 |
| 1 | 3 | is | book | 5.501850344983498e-17 |
| 1 | 4 | is | , | 1.5123035298142912e-16 |
| 1 | 5 | is | that | 5.501850344983498e-17 |
| 1 | 6 | is | is | 3.780758595799811e-17 |
| 1 | 7 | is | a | 1.5123035298142912e-16 |
| 1 | 8 | is | pen | 5.501850344983498e-17 |
| 1 | 9 | is | . | 1.5123035298142914e-16 |
| 1 | 10 | is | 這 | 5.501850344983498e-17 |
| 1 | 11 | is | 是 | 1.5123035298142912e-16 |
| 1 | 12 | is | 一 | 1.5123035298142912e-16 |
| 1 | 13 | is | 本 | 5.501850344983498e-17 |
| 1 | 14 | is | 書 | 5.501850344983498e-17 |
| 1 | 15 | is | , | 1.5123035298142912e-16 |
| 1 | 16 | is | é‚£ | 5.501850344983498e-17 |
| 1 | 17 | is | 是 | 1.5123035298142912e-16 |
| 1 | 18 | is | 一 | 1.5123035298142912e-16 |
| 1 | 19 | is | æž | 5.501850344983498e-17 |
| 1 | 20 | is | ç­† | 5.5018503449834965e-17 |
| 1 | 21 | is | . | 1.5123035298142914e-16 |
i run the program to insert large data and i don't know whether data correctly input or not
,the rule is sourceindex will be from 0 to 21 and each sourceindex follow the targetindex from 0 to 21 , i want to check which row isn't insert by query,how can i do
To do that in a single query may not be the optimal way to do it (why not use several queries?), but this should work:
$combinations = array();
for ($s = 0; $s < 22; $s++) {
for ($t = 0; $t < 22; $t++) {
$combinations[] = $s . '-' . $t;
}
}
$sql = " SELECT CONCAT(sourceindex, '-', targetindex)
FROM table ";
// run your sql query depending on your db layer (e.g. PDO)
// get results as array $results
print_r(array_diff($combinations, $results);
The combinations that get printed are not in the database.
Since there are 22 rows for each sourceindex as well as 22 rows for each targetindex, you could first check, if the number of rows for each index is correct:
SELECT sourceindex, COUNT(*) FROM table GROUP BY sourceindex;
SELECT targetindex, COUNT(*) FROM table GROUP BY targetindex;
If any result row shows a number less than 22 for the count column, there is something missing there. You can than examine further or just combine the possibilities.

mysql subquery count where and group by

I have 4 tables like
cc_agents
+----+----------+
| id | username |
+----+----------+
| 1 | sankar |
| 2 | jenifer |
| 3 | andrew |
| 4 | nirmal |
| 5 | raja |
+----+----------+
cc_callers
+----+-----------+
| id | name |
+----+-----------+
| 1 | sankar |
| 2 | nirmal |
| 3 | jenifer |
| 4 | raja |
| 5 | sankar |
| 6 | office |
| 7 | andrew |
| 8 | sabarish |
| 9 | saravanan |
+----+-----------+
cc_caller_requirement
+----+-------------+--------------+
| id | cc_agent_id | cc_caller_id |
+----+-------------+--------------+
| 1 | 1 | 5 |
| 2 | 1 | 5 |
| 3 | 1 | 2 |
| 4 | 1 | 2 |
| 5 | 1 | 7 |
| 6 | 4 | 2 |
| 14 | 1 | 2 |
| 13 | 5 | 2 |
| 12 | 5 | 2 |
| 15 | 1 | 8 |
| 16 | 1 | 9 |
+----+-------------+--------------+
cc_notifications
+----+-------------+--------------+-------------------+----------------------+
| id | cc_agent_id | cc_caller_id | cc_requirement_id | cc_notification_type |
+----+-------------+--------------+-------------------+----------------------+
| 1 | 1 | 5 | 1 | sms |
| 2 | 1 | 5 | 1 | mail |
| 3 | 1 | 5 | 1 | courier |
| 4 | 1 | 5 | 2 | sms |
| 5 | 1 | 5 | 2 | mail |
| 6 | 1 | 2 | 3 | sms |
| 7 | 1 | 2 | 4 | sms |
| 8 | 1 | 2 | 4 | mail |
| 9 | 1 | 2 | 4 | courier |
| 10 | 1 | 7 | 5 | mail |
| 11 | 1 | 7 | 5 | courier |
| 12 | 4 | 2 | 6 | sms |
| 13 | 4 | 2 | 6 | mail |
| 14 | 4 | 2 | 6 | courier |
| 30 | 5 | 2 | 12 | sms |
| 31 | 5 | 2 | 12 | mail |
| 32 | 5 | 2 | 12 | courier |
| 33 | 5 | 2 | 13 | sms |
| 34 | 5 | 2 | 13 | mail |
| 35 | 5 | 2 | 13 | courier |
| 36 | 1 | 2 | 14 | sms |
| 37 | 1 | 8 | 15 | sms |
| 38 | 1 | 8 | 15 | mail |
| 39 | 1 | 9 | 16 | sms |
| 40 | 1 | 9 | 16 | mail |
+----+-------------+--------------+-------------------+----------------------+
i execute sql query is
SELECT cca.id, cca.username,
(SELECT COUNT(cccr.id)
FROM cc_caller_requirements AS cccr
WHERE cccr.cc_agent_id = cca.id
GROUP BY cccr.cc_caller_id) AS num_of_callers,
(SELECT COUNT(ccns.id)
FROM cc_notifications AS ccns
WHERE ccns.cc_agent_id = cca.id
AND ccns.cc_notification_type_id = 'sms') AS sms,
(SELECT COUNT(ccnm.id)
FROM cc_notifications AS ccnm
WHERE ccnm.cc_agent_id = cca.id
AND ccnm.cc_notification_type_id = 'mail') AS mail,
(SELECT COUNT(ccna.id)
FROM cc_notifications AS ccna
WHERE ccna.cc_agent_id = cca.id
AND ccna.cc_notification_type_id = 'courier') AS courier
FROM cc_agents AS cca
GROUP BY cca.id
I'm looking for output like this:
+------------+---------------+-----------+------------+---------------+
| agent name | no of callers | total sms | total mail | total courier |
+------------+---------------+-----------+------------+---------------+
| sankar | 5 | 7 | 6 | 3 |
| jenifer | 0 | 0 | 0 | 0 |
| andrew | 0 | 0 | 0 | 0 |
| nirmal | 1 | 1 | 1 | 1 |
| raja | 1 | 2 | 2 | 2 |
+------------+---------------+-----------+------------+---------------+
agent name, total sms, total mail and total courier data are working well...
but i get this error " Subquery returns more than 1 row" when i want no of callers
pls help me to solve it
Change your query to:
SELECT cca.id, cca.username,
(SELECT COUNT(DISTINCT cccr.cc_caller_id)
FROM cc_caller_requirements AS cccr
WHERE cccr.cc_agent_id = cca.id
) AS num_of_callers,
(SELECT COUNT(ccns.id)
FROM cc_notifications AS ccns
WHERE ccns.cc_agent_id = cca.id
AND ccns.cc_notification_type_id = 'sms') AS sms,
(SELECT COUNT(ccnm.id)
FROM cc_notifications AS ccnm
WHERE ccnm.cc_agent_id = cca.id
AND ccnm.cc_notification_type_id = 'mail') AS mail,
(SELECT COUNT(ccna.id)
FROM cc_notifications AS ccna
WHERE ccna.cc_agent_id = cca.id
AND ccna.cc_notification_type_id = 'courier') AS courier
FROM cc_agents AS cca
GROUP BY cca.id

sql command to subtract one item from the quantity [duplicate]

This question already has answers here:
subtract "1" from the number in a row SQL Query [duplicate]
(3 answers)
Closed 8 years ago.
I am new in my sql and can not really figure out how to do it.
I have a two tables.
trucks
select * from trucks;
+----+---------+----------+--------+--------------+
| id | size | quantity | status | customers_id |
+----+---------+----------+--------+--------------+
| 12 | 10_feet | 3 | active | 0 |
| 13 | 10_feet | 3 | active | 0 |
| 14 | 10_feet | 2 | active | 0 |
| 15 | 14_feet | 5 | active | 0 |
| 16 | 14_feet | 2 | active | 0 |
| 17 | 14_feet | 2 | active | 0 |
| 18 | 17_feet | 2 | active | 0 |
| 19 | 17_feet | 2 | active | 0 |
| 20 | 24_feet | 3 | active | 0 |
| 21 | 10_feet | 1 | active | 0 |
| 22 | 24_feet | 1 | active | 0 |
+----+---------+----------+--------+--------------+
and truck_customers
select * from truck_customers;
+----+--------+----------+--------+------------------+---------+------------+
| id | first | last | dl | email | size | date_in |
+----+--------+----------+--------+------------------+---------+------------+
| 3 | Poul | Jons | A13324 | poul#gmail.com | 10_feet | 2013-11-30 |
| 4 | Poul | Watson | A23439 | watson#gmail.com | 10_feet | 2013-11-30 |
| 5 | Alex | Snders | A22 | Alex#gmail.com | 17_feet | 2013-11-30 |
| 6 | santes | Garsia | A18337 | Santes#gmail.com | 10_feet | 2013-11-30 |
| 7 | James | Bond | JB111 | Bond#gmail.com | 10_feet | 2013-11-30 |
| 8 | John | Travolta | G123 | Gohn#gmail.com | 14_feet | 2013-11-30 |
+----+--------+----------+--------+------------------+---------+------------+
When entering information into truck_customers, it should automatically subtract
one item from trucks table based on the size.
Using
$sql = " UPDATE trucks SET quantity = -- WHERE size = '$size'";
But it does not work.
I can send you my code if it is going to be easier to understand.
You're almost there.
UPDATE trucks SET quantity = quantity-1 WHERE size = '$size'
Fiddle
Not only would your code not work, but you'd lose your WHERE conditions too...
-- this is a comment in SQL
http://dev.mysql.com/doc/refman/5.0/en/comments.html
You need to assign to original variable plus/minus increment, as Hanky Panky mentioned above.

Concatenate and Count the multiple rows in single rows in mysql

I want to concatenate and count data of the same column, so I can concatenate but I can not count the repeated data.
Here's my table of data:
| ID | bills | class |
|-----|-------|-------|
| 1 | 0.5 | 2 |
| 2 | 1 | 1 |
| 3 | 0.5 | 2 |
| 5 | 1 | 3 |
| 6 | 0 | 2 |
| 7 | 0.5 | 1 |
| 8 | 1 | 2 |
| 9 | 1 | 3 |
| 10 | 0.5 | 1 |
| 11 | 0 | 2 |
| 12 | 1 | 1 |
| 13 | 0 | 3 |
| 14 | 1 | 2 |
| 15 | 0 | 1 |
| 16 | 0 | 1 |
| 17 | 0.5 | 3 |
| 18 | 0 | 3 |
| 13 | 0.5 | 3 |
Here's my sql query I'm using to concatenate data:
SELECT class AS lesson,
GROUP_CONCAT( bills ORDER BY bills ) AS bills
FROM tb_presence
GROUP BY class;
Here's my result below:
| class | bills |
|-------|------------------|
| 1 | 1,0.5,0.5,1,0,0 |
| 2 | 0.5,0,1,0,1 |
| 3 | 1,1,0,0.5,0,0.5 |
Now I would like to count the data that are equal, but continue with the same concatenation.
I want to "count" the data with the same values ​​and display concatenated (column observation and only to help understanding)
| class | bills | observation |
|-------|-------|-----------------------------|
| 1 | 2,2,2 | (2=0+0) (2=0.5+0.5) (2=1+1) |
| 2 | 2,1,2 | (2=0+0) (1=0.5) (2=1+1) |
| 3 | 2,2,2 | (2=0+0) (2=0.5+0.5) (2=1+1) |
Is this really possible?
Here is a solution (thanks to #wchiquito for the sqlfiddle) See http://sqlfiddle.com/#!2/2d2c8/1
As you can see it cannot dynamically determine the bills' values and count them. But there is a count per bill value that you want.
SELECT class AS lesson,
GROUP_CONCAT( bills ORDER BY bills ) AS bills
,SUM(IF(bills=0,1,0)) AS Count0
,SUM(IF(bills=0.5,1,0)) AS Count05
,SUM(IF(bills=1,1,0)) AS Count1
,COUNT(*) AS totalRecords
,COUNT(*)
- SUM(IF(bills=0,1,0))
- SUM(IF(bills=0.5,1,0))
- SUM(IF(bills=1,1,0))
AS Missing
FROM tb_presence GROUP BY class;
I added an extra record to show how the 'missing' column could show if you were not taking all values into consideration.
Results
| LESSON | BILLS | COUNT0 | COUNT05 | COUNT1 | TOTALRECORDS | MISSING |
|--------|-----------------------------------------|--------|---------|--------|--------------|---------|
| 1 | 0.00,0.00,0.50,0.50,1.00,1.00,1.00,4.00 | 2 | 2 | 3 | 8 | 1 |
| 2 | 0.00,0.00,0.50,0.50,1.00,1.00 | 2 | 2 | 2 | 6 | 0 |
| 3 | 0.00,0.00,0.50,0.50,1.00,1.00 | 2 | 2 | 2 | 6 | 0 |

Categories