Join tables with different colums to single colum - mysql

First off, for categorization purposes, could someone figure out a better name to index this as?
Secondly, I have a table and I want to grab the username from another table to two columns on the same table.
SELECT ignite_board.*,username AS 'name'
FROM ignite_board,user_info
WHERE bnumber = 3
AND ignite_board.uid = user_info.id
ORDER BY POSITION
Gets me this:
ID bnumber position UID qual time following name
1176 3 1 442 2 1382045726 440 bb1
1177 3 2 445 2 1382045936 442 bb4
1181 3 3 450 2 1382050220 449 bb6
1178 3 4 446 2 1382050371 439 aa2
1209 3 5 466 1 1382050130 450 bb8
1212 3 6 469 2 1382050502 467 bb10
1210 3 7 467 1 1382050172 466 bb9
1232 3 8 475 1 1382050415 446 aa7
1180 3 9 444 0 1382045690 445 bb3
1214 3 10 471 0 1382050220 466 bb11
1233 3 11 476 0 1382050415 475 aa8
1179 3 12 441 0 1382045562 475 aa1
1182 3 13 452 0 1382046032 448 aa6
1216 3 14 473 0 1382050272 469 bb12
1234 3 15 477 0 1382050502 469 bb16
1271 3 16 454 0 1382306814 442 bb7
But what would I need to get the username and the Following username (sorta like this)
SELECT ignite_board.*,username AS 'name',username AS 'following'
FROM ignite_board,user_info
WHERE bnumber = 3
AND ignite_board.uid = user_info.id
AND ignite_board.following = user_info.id
ORDER BY POSITION

SELECT b.*
, u.username name
, f.username following
FROM ignite_board b
JOIN user_info u
ON u.id = b.uid
JOIN user_info f
ON f.id = b.following
WHERE bnumber = 3
ORDER
BY position;

You have to join the user_info table twice, and give a different alias to each instance:
SELECT ignite_board.*,u.username AS 'name',f.username AS 'following'
FROM ignite_board,user_info u, user_info f
WHERE bnumber = 3
AND ignite_board.uid = u.id
AND ignite_board.following = f.id
ORDER BY POSITION

Related

MySQL: select sum of column having specific values

What I'm trying to do is to select a specific amount of tickets (max 2) and every person that has the sum of the number of tickets less of 3 and the valid field has to be != 'e'
I have this table:
ID
id_person
nr_tickets
valid
1
220
1
s
2
220
1
s
3
330
2
s
4
330
1
e
5
331
1
s
6
220
2
s
7
441
1
s
8
442
2
s
9
443
1
s
10
444
1
s
11
445
2
s
Here is what I did:
SELECT m.id, m.id_person, m.nr_tickets, m.valid
FROM table m
JOIN table m1 ON m1.id <= m.id
WHERE m.nr_tickets > 0
GROUP BY m.id
HAVING SUM(case when m.valid != 'e' then m1.nr_tickets end) <= 10
This query gives me
ID
id_person
nr_tickets
valid
1
220
1
s
2
220
1
s
3
330
2
s
5
331
1
s
6
220
2
s
7
441
1
s
8
442
2
s
As you can see the query it's almost right, the thing is that the person 220 in the results has the sum of the tickets is greater than 2.
What I'm trying to achieve is to bypass the ID 6, and to have instead the ID 9
select `id`,`id_person`, sum(`nr_tickets`) as `nr_tickets`, `valid`
from `test`
group by `id_person`
having sum(`nr_tickets`) < 3 and `valid`!="e"
Output:
id id_person nr_tickets valid
5 331 1 s
7 441 1 s
8 442 2 s
9 443 1 s
10 444 1 s
11 445 2 s

SQL Query for Max Value of Grouping Till Date for All Dates

I have some values which is based on date for different groupings
For each grouping id, what is the max of value till a particular date.
Example
Date Grouping Id Value
2017-06-01 1 100
2017-06-03 1 101
2017-06-06 1 103
2017-06-02 2 200
2017-06-04 2 210
2017-06-08 2 220
2017-06-01 2 1000
2017-06-05 2 1020
2017-06-09 2 1050
I need the output like below
Date Grouping Id Value
2017-06-01 1 100
2017-06-02 1 100
2017-06-03 1 101
2017-06-04 1 101
2017-06-05 1 101
2017-06-06 1 103
2017-06-07 1 103
2017-06-08 1 103
2017-06-09 1 103
2017-06-10 1 103
2017-06-01 2 NULL
2017-06-02 2 200
2017-06-03 2 200
2017-06-04 2 210
2017-06-05 2 210
2017-06-06 2 210
2017-06-07 2 210
2017-06-08 2 220
2017-06-09 2 220
2017-06-10 2 220
2017-06-01 3 1000
2017-06-02 3 1000
2017-06-03 3 1000
2017-06-04 3 1000
2017-06-05 3 1020
2017-06-06 3 1020
2017-06-07 3 1020
2017-06-08 3 1020
2017-06-09 3 1050
2017-06-10 3 1050
So assuming you have the TEMPLATE_TABLE that has all days x all grouping IDs data, you can try below two queries:
CREATE TABLE TABLE_1 AS
SELECT A.DATE, A.GROUPING_ID, B.VALUE
FROM
TEMPLATE_TABLE A
LEFT JOIN
GIVEN_TABLE B
ON A.DATE = B.DATE AND A.GROUPING_ID = B.GROUPING_ID
ORDER BY A.GROUPING_ID, A.DATE;
Above table TABLE_1 will pad values for matches and NULL for non-matches.
You can use this table to find maximum values till a given date for all dates through a self join given in the below query:
SELECT B.DATE, B.GROUPING_ID, MAX(A.VALUE) AS VALUE
FROM
TABLE_1 A
INNER JOIN
TABLE_1 B
ON A.GROUPING_ID = B.GROUPING_ID AND A.DATE<=B.DATE
GROUP BY B.DATE, B.GROUPING_ID;
Hope this works. Let me know if this doesn't.

list all students who have written atmost one exam per day

I have 3 tables likes Student,Subject,and Midterm tables.
Student table contains
studid Firstname lastname Class
1 A R 12A
2 B S 12A
3 C T 12A
4 D U 12A
5 E V 12B
SUBJECT table contains
subid subname
1 maths
2 science
3 english
MIDTERM table contains
studid subid marks examdate
1 1 100 2014-09-24
1 2 92 2014-09-25
1 2 92 2014-09-26
2 1 74 2014-09-24
2 2 78 2014-09-26
2 3 73 2014-09-26
3 1 90 2014-09-24
3 2 84 2014-09-25
3 2 92 2014-09-25
5 1 87 2014-09-24
4 2 79 2014-09-24
4 3 90 2014-09-26
select * from Student
EXCEPT
--list of students with more than one subject per day
select A.* from Student A
join
(select studid, examdate, count(distinct subid)cnt group by studid, examdate from [Midterm]
)B
on A.Studid = B.Studid
and B.cnt >1

Fetch Data from master table based on child table condition in sql server

Need to fetch data from master table based on child table condition.
Master Table:-
ID Name Address
1 abc xyz
2 abs txt
3 aui tre
4 pop top
5 the tre
6 pot tos
7 pog sop
8 pat top
9 bat cric
10 not kot
Child Table:-
chid shootid imagename IDFK
101 234 123ab.jpg 3
102 234 54abcab.jpg 3
103 235 123abc.jpg 3
104 236 12390acb.jpg Null
105 235 12332aab.jpg 8
106 234 123786ab.jpg 4
107 234 54789abcab.jpg 10
108 235 122343abc.jpg 10
109 235 122123acb.jpg 4
110 234 12123aab.jpg 9
111 234 1223ab.jpg Null
112 233 5432abcab.jpg Null
113 235 1239abc.jpg Null
114 236 1238acb.jpg 2
115 236 12356aab.jpg 2
116 236 1235ab.jpg 2
117 236 545abcab.jpg Null
118 237 1233abc.jpg 1
119 237 1223acb.jpg 1
120 237 1123aab.jpg 1
In Child table IDFK is the foreign key and ID in Master table is the primary key of that.
Now i want to show those name from master table that doesn't exist on child table filter on shootid like where childtable.Shootid=234. I tried but not find the desired output.Every time it just return's the same out for different shootid as well.
Please help me and show me the right query for that.
I don't know if I am understand you well or not but I think this is what you want,
Select * from [master] m
where m.ID not in (Select IDFK from detail where shootid=234)
I think this is what you are looking for.
Select distinct m.name from master m LEFT OUTER JOIN child c
ON m.id = c.id and
c.shootid=234
where
c.id is null

fetch records one by one from each category in the table

table structure is as following.
item_id, item_name, dealer_id
this table has a lot of number of records. Having different number of records for different dealer.
in the item list , there are all the items having paging.
but i want to show all item on a page from different dealers. if there are no more items for unique dealers then it can show multiple items for same dealer.
how can i get this ?
table data is as following :
ITEM_ID ITEM_NAME DEALER_ID
1 a 215
2 b 500
3 c 517
4 d 215
5 e 518
6 f 517
7 g 215
8 h 565
9 i 517
10 j 215
11 k 500
12 l 247
13 m 215
14 n 530
15 o 517
16 p 215
17 q 500
I want them in following order - it is sorted on dealer id.
ITEM_ID ITEM_NAME DEALER_ID
1 a 215
12 l 247
2 b 500
3 c 517
5 e 518
14 n 530
8 h 565
16 p 215
17 q 500
9 i 517
7 g 215
11 k 500
15 o 517
13 m 215
6 f 517
4 d 215
10 j 215
although it is sorted on dealer but in the result set first look for items from different dealers. if there are no more items from different dealers then it can have from same dealer.
It's very hard to tell what you want, but I think it might be this:
select ITEM_ID, ITEM_NAME, DEALER_ID
from mytable
order by ITEM_NAME, DEALER_ID