I want to get results from some specific values only. For example, if I have a table like this
Id
Name
1
A
2
A
3
B
4
B
I want results like this.
Id
Name
1
A
3
B
4
B
Which is the group by only on rows which have values A. Is this possible in a single query?
Related
I have two tables:
Table A: ID Items Data Pos
Table B: ID Apples Oranges Pos
And what I need to get all results from Table A and B ordered by position.
How can I do this? SELECT * FROM Table a and b orderby pos?
So for example the result should look like this:
Result from Table A with Id 1 Items 10 Data 5 and Pos 1
Result from Table B with Id 1 Apples 3 Oranges 3 and Pos 2
Result from Table B with Id 2 Items 4 Data 4 and Pos 3
Result from Table A with Id 2 Apples 7 Oranges 8 and Pos 4
Thank you.
You would use union all with an order by:
select ID, Items, Data, Pos
from tableA a
union all
select ID, Apples, Oranges, Pos
from tableB b
order by Pos
This is standard SQL so it will work in all the databases you mention.
I'm a new in Mysql and I have a complicated problem:
I have a table with "Shops" name in this table there is a ShopID column. The records look like this:
Shop_001
Shop_002...
Every "shopID" refer to a new table with this name, for example there is a table with Shop_0001 name. In this table there is "partnumber" column which mean the parts which are available in this shop.
I send a specific part number to sql server and I want to check all shops in the "Shops" table and return a rows in the "Shop_xxxx" tables which has this specific partnumber.
Unfortunately I have no idea how do I get start on this. Can anybody help me give some instruction or anything on this?
you're looking for a many to many relationship. so you just need 3 tables
1 table is the list of shops
1 table is the list of products
and 1 table is the list of which shops have which products. like this
table1
id|shops
------
1 shop1
2 shop2
3 shop3
table2
id|products
------
1 prod1
2 prod2
3 prod3
4 prod4
5 prod5
table3
id|shop_id|prod_id
-------------------
1 2 3
2 2 1
3 2 2
4 1 3
5 1 4
6 1 5
7 3 2
So for every time a product is added to a shop, an entry is added in table3. This will allow you to query by shops or by products, and you will only ever need 3 tables.
google querying many to many relationships for how to get the list of products for shop1 or the list of shops that have product4 etc.
I have created a database and website that will be used by football managers to select their team etc. Once a match has been completed events will be stored in the match_players table. Such events are Goal, Yellow Card, Red Card etc. I have no problem getting this information into php from SQL db.
I need to add up how many times a Goal appears (a '1' is placed in the SQL table) and for what team so that a final score can be displayed. So, for example, if Team A has 1 goal and Team B has 2 then I need to display that. I am trying to count the amount of times that a Goal is registered in the table. Any help will be greatly appreciated.
You can use MYSQL SUM
select SUM(Goal) from match_players where Team="A"
Or you can get the same for all teams by
select Team,SUM(Goal) from match_players group by Team
Why don't you demand this sum to SQL directly?
SELECT SUM(goals)
FROM match_table
WHERE team = 'Barcellona'
This should be much faster also than elaborate all data at "php-level"
If you want this detail for all teams
SELECT team,SUM(goals)
FROM match_table
GROUP BY team
Well if you store a 1 each time a goal is scored, your table looks like this:
TeamID goal
1 1
2 1
1 1
3 1
2 1
2 1
1 1
So you just want a count of how many times a team appears in that table:
select TeamID, count(*) from table group by TeamID
Will give you
TeamID | count(*)
1 | 3
2 | 3
3 | 1
how can I count Mysql Field Rows?
For example I have table called student_attendance where we have 4 fields: absent, present, holiday, leave.
There is list of 10+ students, each student value will go to its own field, like if for one student we selected absent, 1 value will go to absent field and if someone select present, 1 value will go to present field.
Now what i want is
Each time attendance take, new row insert in student_attendance table for the student.
So if there are 10 Rows in student_attendance table, how can i + all of them
Like if there are 10 rows of present field, 3 rows are empty and 7 rows have 1 value, how can i count it so that the total value of specific field 1+1+1+1+1+1+1 can comes in php so it show 7 ?
SELECT SUM(present) AS presence_days
FROM student_attendance
This will result:
+---------------+
| presence_days |
+---------------+
| 7 |
| |
+---------------+
Example SQL (Demo):
select count(*) from table_name where present_field=1;
Try this:
SELECT SUM(absent), SUM(present), SUM(holiday), SUM(leave)
FROM student_attendance
GROUP BY sudentId;
If I have two tables:
Temp Snow
-------- -------------
School School Skip
-------- -------------
School 1 School 1 1
School 2 School 4 0
School 3 School 3 1
And I want to see if a 0 is present in the Skip column of table Snow, is it possible to just in the rows that contain the same School value? In this case, it would just search for a 0 in the School 3 and School 1 rows, because the School name matches up with the one from Temp.
Currently, I am using the following, but it's including every row:
SELECT Skip FROM Snow WHERE Skip = 0
You just need to join both tables. As you can see there are letters after the table names. They are called ALIASes (nickname) of the tables.
SELECT a.School
FROM Snow a
INNER JOIN Temp b
ON a.School = b.School
WHERE a.skip = 0
SQLFiddle Demo