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.
Related
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?
This question already has answers here:
MySQL join with where clause
(3 answers)
Closed 5 years ago.
I have two tables
1st table :-
id name dept
1 John dept1
2 Mary dept2
3 Dave dept3
4 John dept4
5 John dept5
2nd table :-
id submitter dept
1 Rupert dept3
2 Joe dept1
3 Lisa dept2
4 Louise dept4
5 Tom dept5
what i would like is a query to allow people in the name column in the first table to only show records based on their matching departments eg John in table one will return the 3 records in table 2 (id 2,4 and 5)
So far i have tried SELECT * FROM table1, table2 WHERE table1.dept = table2.dept AND table1.name='John'
If I correctly understand your problem, you need to make a join between the two tables using the field dept and filter your results by the name of the requester, in the first table.
SELECT t2.submitter, t2.dept
FROM table1 t1
LEFT JOIN table2 t2 ON t1.dept = t2.dept
WHERE t1.name = :person_name
Documentation and examples (you can also look at the left menu at Inner, right, full and self join's).
here is my problem,
Imagine this table
id| idcompany
----------
1 | 1
----------
2 | 2
----------
3 | 1
----------
5 | 1
----------
6 | 2
I did a code in PHP like this:
if id <= 2 then DAY1
if id BETWEEN 3,4 then DAY2
if id BETWEEN 5,6 then DAY3
Looking to the table we can assume that company 1 appears on Day1, Day2 and Day 3
and company 2 appears Day1 and Day3
What I want to accomplish here is: How can I SELECT in SQL all the companies
who won't participate on Day 2?
I already tried:
SELECT * FROM tickets
WHERE id NOT BETWEEN 3 AND 4
GROUP BY(idcompany)
But its obvious that wont work because the Query will select the idcompany from another days and print on result.
Can someone help me to fig this out?
Thanks in advance.
Hope, I got you correctly
http://sqlfiddle.com/#!9/6f9921/1
SELECT idcompany
FROM tickets
GROUP BY idcompany
HAVING SUM(IF(id IN (3,4),1,0))=0
UPDATE If you need an interval you can replace condition in SUM:
SELECT idcompany
FROM tickets
GROUP BY idcompany
HAVING SUM(IF(id BETWEEN 330601 AND 40800,1,0))=0
To get a list of companies who didn't participate on day two, start by getting a list of those who did:
SELECT DISTINCT companyID
FROM myTable
WHERE id BETWEEN 3 AND 4;
Then you can exclude them from the final result by selecting all companies, and using a NOT IN operator to filter those out:
SELECT DISTINCT companyID
FROM myTable
WHERE companyId NOT IN(
SELECT DISTINCT companyID
FROM myTable
WHERE id BETWEEN 3 AND 4);
Here is an SQL Fiddle example.
Your table is rather confusing, I can't seem to find the information where Company 1 appears on Day 2.
But if I have understood correctly, here is a simple NOT or != operand for the WHERE operator example:
SELECT * FROM tickets
WHERE id != 3 AND id != 4
how i can count the repeated value in any field of mysql db's table.
Example:
id name
1 aaa
2 aaa
3 ttt
4 ccc
5 ttt
6 ccc
7 aaa
8 zzz
How i can get how many times value is repeated in table like.
aaa =3 times in table
ttt =2 times in table
ccc =2 times in table
zzz =1 times in table
I think it possible with count of the mysql but how to use it i dont know any one can help ?please answer ma question thanks in adv.
You need to group by your name column and then you can use an aggregate function like count() on that group
select name, count(id)
from your_table
group by name
Write below query to get count of each name
select name, count(name)
from your_table
group by name
OR
to get count of specific name
select name, count(name)
from your_table
where name = "aaa"
group by name
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