Is this possible?
INSERT INTO dfjw_favorite_list (`term_id`,`added_at`,`user_id`) SELECT
trans.id , '1402309310' , '1' FROM dfjw_terms t,dfjw_language_groups g,
dfjw_terms trans WHERE t.id = '11800' AND g.language_group_id =
t.language_group_id AND trans.language_group_id= g.id
After that I want to select from another table but I want to do this inside 1 INSERT Statement. Like this.. but how can I combine it?
SELECT id,'1402309310','1' FROM terms WHERE id = '11800';
best regards and thanks in advance
You may use union all
INSERT INTO dfjw_favorite_list (`term_id`,`added_at`,`user_id`)
SELECT
trans.id , '1402309310' , '1'
FROM dfjw_terms t,dfjw_language_groups g,
dfjw_terms trans
WHERE
t.id = '11800'
AND g.language_group_id = t.language_group_id
AND trans.language_group_id= g.id
union all
SELECT id,'1402309310','1' FROM terms WHERE id = '11800';
Related
I want to use mysql's "JOIN"
I want to group rows by "date_text" where "tokenIdx" is "1001" and "datetime_unix" is the highest value.
Is my code wrong?
SELECT `A.idx`
FROM `data_candle_h1` 'A'
JOIN
(
SELECT `date_text`, MAX(`datetime_unix`) AS 'datetime_unix'
FROM `data_candle_h1`
WHERE `tokenIdx` = '1002'
GROUP BY `date_text`
) 'B'
ON `A.datetime_unix` = `B.datetime_unix`
WHERE `A.tokenIdx` = '1002'
Your query is syntactically perfect. Just remove single quotes('') around table aliases (A and B). I have corrected it. Please check this out.
SELECT `A.idx`
FROM `data_candle_h1` A
JOIN
(
SELECT `date_text`, MAX(`datetime_unix`) AS 'datetime_unix'
FROM `data_candle_h1`
WHERE `tokenIdx` = '1002'
GROUP BY `date_text`
) B
ON `A.datetime_unix` = `B.datetime_unix`
WHERE `A.tokenIdx` = '1002'
review table has store_idx, user_idx etc...
I want to create a query sentence that gets information about the store to which the user has bookmarked with the user_id value entered.
The query sentence I made is
select A.store_name
, A.store_img
, count(B.store_idx) as review_cnt
from board.store A
Left
Join board.review B
On A.store_idx is B.store_idx
where store_idx is (select A.store_idx from bookmark where user_id = ?)
However, nothing came out as a result.
Help me..
Please use below Query:
SELECT store_name
, store_img
, SUM(review_cnt) AS review_cnt
FROM
( SELECT DISTINCT A.store_name
, A.store_img
, CASE WHEN B.store_idx IS NULL THEN 0 ELSE 1 END AS review_cnt
FROM bookmark br
JOIN board.store A
ON A.store_idx = br.store_idx
LEFT
JOIN board.review B
ON A.store_idx = B.store_idx
WHERE br.user_id = ?
)T
The WHERE clause is obviously filtering out all rows. We can't do much about that. But your query is also lacking a GROUP BY, the table aliases can be improved, and the join condition is not correct.
So, try this version:
select s.store_name, s.store_img, count(b.store_idx) as review_cnt
from board.store s left join
board.review r
on s.store_idx = r.store_idx
where b.store_idx in (select b.store_idx
from bookmark b
where b.user_id = ?
);
I have 2 tables namely ItemList table and ItemChara table, having this kind of structure:
wherein, Item_Num of Item Chara is a foreign key of Item List table. Now I'm trying to JOIN these tables. With a given like these.
How can I JOIN these tables with those values to form something Like this
I'm trying to find out what is the simplest way to achieve this. I limit the colors in table Item Chara at maximum of 2 colors per Item so it won't exceed 2 colors per item. I want to make another column like Color1 and Color2 to get each color in each item. If it happens that the item doesn't have a color to pair up with, it would just left blank, or null maybe.
Sorry but I'm not sure what to call the thing that I want to do but I know the output that I want to have, so the question title might be irrelevant. I'll change it as soon as I found out.
One way to do this is to use the row_number() window function:
select
i.item_num, i.item_name,
max(case when rn = 1 then ic.chara_color end) color1,
max(case when rn = 2 then ic.chara_color end) color2
from itemlist i
join (
select
item_num, chara_color,
rn = row_number() over (partition by item_num order by chara_num)
from itemchara
) ic on i.item_num = ic.item_num
group by i.item_num, item_name;
Below code will give u the required result.
select A.item_num,A.Item_name,
max(Color_1) AS Color_1,
max(Color_2) As Color_2
from (
select il.item_num,il.Item_name,
(case when ic.chara_num = 1 then ic.chara_color END) AS Color_1,
(case when ic.chara_num = 2 then ic.chara_color END) AS Color_2
from Item_List il inner join Item_Chara ic on il.Item_Num = ic.Item_Num
) A
group by item_num,Item_name
This code is not tested may be have some syntax erro which u need to check and sort it out. Let me know if still ur facing some issue.
Similar to other answers, but since I already made it I might post it anyway.
;with itemlist(itemId, itemName)
as
(
select 1,'Bag'
union ALL
select 2,'Pen'
union ALL
select 3,'Bike'
union ALL
select 4,'Shoes'
)
,itemchara(charaId, itemId, charaColor)
as
(
select 1, 1, 'Blue'
union all
select 2, 1, 'Red'
union all
select 3, 2, 'Black'
union all
select 4, 2, 'Blue'
union all
select 5, 3, 'Green'
union all
select 6, 4, 'Black'
)
,tmp
as
(
select i.itemName
,c.*
,row_number () over ( partition by c.itemId order by c.charaId ) as r
from itemlist i
join itemchara c
on i.itemId = c.itemId
)
select t1. itemId
,t1.itemName
,t1.charaColor
,t2.charaColor
from tmp t1
left join tmp t2
on t1.r+1 = t2.r
and t1.itemId = t2.itemId
where t1.r = 1
I need to Find out the course with the most passes from my table tblResults.
tblResults:
StuID Course Symbol
1001 CSC101 P
1001 RNG101 F
1002 CSC101 P
1002 RNF101 F
1003 HAP101 P
1004 HAP101 P
i.e should give CSC101 (And all other courses (HAP101) with the same ammount of passes)
I have tried:
CREATE VIEW Part1 AS
SELECT NumbF
FROM
(SELECT COUNT(Course) AS NumbP,
Course
FROM tblResults
WHERE Symbol = 'P')
GROUP BY Course);
CREATE VIEW Part2 AS
SELECT MAX(NumbP) AS Maxnum
FROM Part1,
tblResults
WHERE Symbol = 'P'
GROUP BY Course;
SELECT Part1.Course
FROM Part1,
Part2
WHERE Part1.NumbP = Part2.MaxNum
But I seem to be doing something incorrectly. Please help
Something like this should work:
create view yourview as
select course, count(*) passcnt
from tblResults
where symbol = 'P'
group by course
select *
from yourview
where passcnt = (select max(passcnt) from yourview)
SQL Fiddle Demo
Note, you don't need the view, I just left it for simplicity.
SELECT Course, count(StuID) count
FROM tblResults
GROUP BY Course
HAVING count = (SELECT max(c)
FROM (SELECT count(StuID) c, Course
FROM tblResults
GROUP BY Course
WHERE Symbol = 'p'
) counts
);
You can try the SELECT below, but I haven't tested it:
SELECT selA.Course, selA.NumPass FROM (
SELECT Course, COUNT(Course) AS NumPass FROM
tblResults GROUP BY Course HAVING Symbol = 'P') AS selA
INNER JOIN (
SELECT MAX(NumPassCount) AS NumPassMax FROM (
SELECT COUNT(Course) AS NumPassCount FROM
tblResults GROUP BY Course HAVING Symbol = 'P') AS selB)
AS selC ON selA.NumPass = selC.NumPassMax
Using a couple of sub queries
SELECT Sub3.Course
FROM
(
SELECT MAX(Course) AS MaxCourseCnt
FROM
(
SELECT Course, COUNT(*) AS CourseCnt
FROM tblResults
WHERE Symbol = 'P'
GROUP BY Course
) Sub1
) Sub2
INNER JOIN
(
SELECT Course, COUNT(*) AS CourseCnt
FROM tblResults
WHERE Symbol = 'P'
GROUP BY Course
) Sub3
ON Sub2.MaxCourseCnt = Sub3.CourseCnt
--This is the code
create table #Test (Systemtraceno nvarchar(50),Bin nvarchar(50),SwitchCode nvarchar(50),
SwitchDesc nvarchar(50),[Description] nvarchar(50))
insert into #Test
select SystemTraceno , Bin,SwitchCode,SwitchDesc,[Description]
from(
select A.SystemTraceNo, A.BIN,'' SwitchCode, '' SwitchDesc,''[Description]
from ATM035 A
where a.TranDate = '20130924' and MsgType = '0210' and TerminalID = '08880001'
and A.ProcessCode in ('011000','012000','013000') and A.ResponseCode = '0000' and A.BIN <> '502265'
--group by A.SystemTraceNo, A.BIN
)x
group by SystemTraceNo,BIN,SwitchCode,SwitchDesc,[Description]
having COUNT(SystemTraceNo)=2
update #Test set SwitchCode = (select top 1 SwitchCode from ATM027 where Bin = #Test.Bin )
update #test set SwitchDesc = (select switchname from ATM016 where SwitchCode = #test.switchcode)
update #test set [Description] = (Select top 1 Description from ATM027 where BIN = #Test.Bin )
Select * from #test order by SwitchDesc asc
drop table #test
--,'301000','302000','303000'
i just wanted to select rows having SystemTrace number count = 2.I earlier had problems with aggregation and now this.Hoping you could help me out.Thanks in advance
#Royi: actually, i think it should be
select customers.customerId , count(orders.id) as num
from customers join orders on custumers.Id = orders.customerId
group by customers.customerId
having num=2
I'm not going to get into your code but this is how it should be :
select customers.customerId
from customers join orders on custumers.Id = orders.customerId
group by customers.customerId
having count(orders.id)=2
See section 2 here : (section 1 is for the one who said it's possible).
http://i.stack.imgur.com/aE2M3.png
where systemtraceno in (select systemtraceno from #Test group by systemtraceno having count(systemtraceno) = 2)
I thin the problem is at
group by SystemTraceNo
when you want to use
COUNT(SystemTraceNo)=2
In short your insert code should be like this:
insert into #Test
select A.SystemTraceno , A,Bin,'' SwitchCode,'' SwitchDesc,'' [Description]
from ATM035 A
where a.TranDate = '20130924' and MsgType = '0210' and TerminalID = '08880001'
and A.ProcessCode in ('011000','012000','013000') and A.ResponseCode = '0000' and A.BIN <> '502265'
group by A.BIN
having COUNT(SystemTraceNo)=2