I have three following tables.
table 1)
| ID_t1 | name1 | price |
| ------ | --------- | ----- |
| 1 | AAA | 100 |
| 2 | ABB | 200 |
| 3 | ACC | 300 |
table 2)
| ID_t2 | name2 |
| ------ | --------- |
| 1 | pig |
| 2 | fog |
| 3 | dog |
table 3)
| ID_t3 | ID_t1 | ID_t2 | quantity |
| ------ | --------- | ----- | -------- |
| 1 | 1 | 1 | 50 |
| 2 | 2 | 1 | 60 |
| 3 | 2 | 2 | 100 |
| 4 | 2 | 3 | 110 |
| 5 | 3 | 2 | 150 |
| 6 | 3 | 3 | 140 |
I want to query data form phpmyadmin to array and i have duplicate values so how to combine duplicate values in object like this. Am new to using PHP.
Related
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.
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
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.
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 |
I have three tables below that shows the student records, subjects and students with subjects.
I would like to ask what is the effective SQL query to show the results below. I can show it using JOIN but not with the format below.
+------+-----------+-----------+-----+----------+-----------+--------+
| Name | Address | Telephone | Sex | Subjects | Teacher | Active |
+------+-----------+-----------+-----+----------+-----------+--------+
| John | somewhere | 12345 | M | | Teacher 1 | YES |
| John | somewhere | 12345 | M | Math | | YES |
| John | somewhere | 12345 | M | Science | | YES |
| John | somewhere | 12345 | M | English | | YES |
| Matt | somewhere | 123456 | M | | Teacher 2 | YES |
| Matt | somewhere | 23456 | M | Math | | YES |
| Matt | somewhere | 123456 | M | Science | | YES |
| Girl | somewhere | 5431 | F | | Teacher3 | YES |
| Girl | somewhere | 5431 | F | Physics | | YES |
| Girl | somewhere | 5431 | F | Math | | YES |
+------+-----------+-----------+-----+----------+-----------+--------+
select * from student_record;
+------------+------+-----------------+-----------+-----+----------+--------+
| id_student | name | address | telephone | sex | teacher | active |
+------------+------+-----------------+-----------+-----+----------+--------+
| 1 | John | Somewhere | 12345 | M | Teacher | 0 |
| 2 | Matt | Somewhere There | 12345222 | M | Teacher1 | 0 |
| 3 | Girl | Somewhere here | 3333 | F | Teacher2 | 0 |
+------------+------+-----------------+-----------+-----+----------+--------+
select * from subjects;
+------------+--------------+---------------------+
| id_subject | subject_name | subject_description |
+------------+--------------+---------------------+
| 1 | Math | Math |
| 2 | Science | Science |
| 3 | English | English |
| 4 | Physics | Physics |
+------------+--------------+---------------------+
select * from with_subjects;
+--------------------+--------------------+------------+
| id_student_subject | student_id_subject | student_id |
+--------------------+--------------------+------------+
| 1 | 1 | 1 |
| 2 | 2 | 1 |
| 3 | 3 | 1 |
| 4 | 4 | 1 |
| 5 | 4 | 2 |
| 6 | 3 | 2 |
| 8 | 1 | 2 |
| 9 | 1 | 3 |
| 10 | 2 | 3 |
| 11 | 3 | 3 |
| 12 | 4 | 3 |
+--------------------+--------------------+------------+
how about
select a.name as "Name",a.address as "Address",a.telephone as "Telephone" ,a.sex as "Sex",null as "Subject",a.teacher as "Teacher",a.active as "Active" from student_record as a
union a.name as "Name",a.address as "Address",a.telephone as "Telephone" ,a.sex as "Sex",b.subject_name as "Subject",null as "Teacher",a.active as "Active" from (student_record as a inner join with_subjects as c on a.id_student = c.student_id) inner join subjects as b on c.student_id_subject = b.id_subject
Not tested it. It wotn be in the same order as your example, but should have all of the data there