I am learning SQL so go easy!
I have a couple of tables. The common column between them is a 'user_id' column. Table 1 has a 'user_name' column. I want to get all the user names from this column in Table 1 where the user_IDs match and update a column in Table 2 named 'domain_info' with: ('domain name\','user_name').
'domain name\' will be text string and
'user_name' will be from Table 1
All help is much appreciated
Cheers
You probably should not update that column because then you would have to keep it updated every time the user_name changes on the user table. This is one of the important concepts of Database normalization regarding dependencies and redundancy. Instead, stick to just querying the data you need from the tables using their relations.
select u.user_name, domain_info = d.domain_info+'\'+u.user_name
from table_1 u
inner join table_2 d
on u.user_id = d.user_id
The update syntax would be:
update d
set domain_info = d.domain_info+'\'+u.user_name
from table_1 u
inner join table_2 d
on u.user_id = d.user_id
If either are nullable, you can wrap them in isnull() or coalesce() like so:
update d
set domain_info = isnull(d.domain_info,'')+isnull('\'+u.user_name,'')
from table_1 u
inner join table_2 d
on u.user_id = d.user_id
Thanks SqlZim..
I got it to work like this:
UPDATE
table2
SET
table2.domain_info = user_name
FROM
table1
WHERE
table1.user_id = table2.user_id;
UPDATE
table2
SET
table2.domain_info =
(SELECT {fn CONCAT('domainname\',domain_info)})
WHERE
user_id = user_id;
I'd be almost certain there is a better syntax for this...I haven't written much TSQL at all. But it worked!
Thanks again
Related
I wasn't sure how to actually title this question, but if you got in here, I'll try to explain it to the best of my ability.
You can find my SQL Fiddle here.
SELECT *
FROM `challenges`
LEFT JOIN `challenges_competitors` ON `challenges_competitors`.`challenge_id` = `challenges`.`id`
LEFT JOIN `users` ON `users`.`id` = `challenges_competitors`.`user_id`
WHERE `challenges`.`owner_id` != 3 AND `challenges`.`status` = 'pending'
ORDER BY `challenges`.`id`;
Basically what I want to do with the query is return any challenge that user3 does not own or is part of.
There are two tables involved for this. The first table is challenges. This table holds the user id as owner_id, as well as the challenge id and some other data. Then there's the challenges_competitors table that holds challenge_id and user_id to connect that table with both challenges and the users.
When I run the query and join the tables, there are bound to have duplicates because a challenge can have many competitors. So what I want to do is if there is a challenge that the user3 does not own, but he is part of this challenge, to not get that row back.
I really hope I explained this well. lol
To get the most basic information about which challenges (their id) aren't being owned by users.id = 3 and that don't have that user as a competitor altering your query by adding NOT EXISTS clause would be sufficient.
SELECT c.id
FROM `challenges` AS c
WHERE
c.`owner_id` <> 3 -- discard challenges that have user 3 as their owner
AND c.`status` = 'pending'
AND NOT EXISTS ( -- discard challenges that have user 3 as their competitor
SELECT 1
FROM `challenges_competitors` AS cc
WHERE
cc.`user_id` = 3 -- limit this query to return only rows where user 3 is a competitor
AND cc.`challenge_id` = c.`id` -- join condition with challenges table
);
Here's your modified SQL FIDDLE.
To get the entire set of columns that you have attached in your query you could keep the joins, as #xQbert suggested in his comment.
SELECT *
FROM `challenges` c
LEFT JOIN `challenges_competitors` cc
ON cc.`challenge_id` = c.`id`
LEFT JOIN `users` u ON u.`id` = cc.`user_id`
WHERE
c.`owner_id` != 3
AND c.`status` = 'pending'
AND NOT EXISTS (
SELECT 1
FROM `challenges_competitors` cc2
WHERE cc.`id` = cc2.`id` and cc2.`user_id` = 3
)
ORDER BY c.`id`;
Because owner is also a row in the user table you need to include the user table as an alias. I'm not quite sure what you need to get back - I don't understand exactly what the question is, but you should be able to add a where clause to this to get what you want.
SELECT *
FROM `challenges`
JOIN `challenges_competitors` ON `challenges_competitors`.`challenge_id` = `challenges`.`id`
JOIN `users` ON `users`.`id` = `challenges_competitors`.`user_id`
JOIN `users` AS `owners` ON `owners`.`id` = `challenges`.`owner_id`
I have two tables that are related to each other.
One is user_registration and the other is user_group.
Inside the user_registration table, I have the fields:
id
id_user
date_begin
date_end
And user_group:
id
id_user
id_group
I have just tried to learn simple join statements to select some records based on their group id like:
SELECT a.id_user, a.date_begin, a.date_end, b.id_group
FROM user_registration as a
INNER JOIN user_group as b
ON a.id_user=b.id_user
WHERE b.id_group = '14'
But right now, what I am trying to do is to set the date_end to be the same as the date_begin where in the id_group of the records will be the same.
I already got it. This did the trick although I am not sure this is the most efficient way as I am still learning MySQL.
UPDATE user_registration AS a
INNER JOIN user_group AS b ON a.id_user = b.id_user
SET a.date_end = a.date_begin
WHERE b.id_group = '14'
As mentioned on the edit above, this accomplished what I was trying to do.
UPDATE user_registration AS a
INNER JOIN user_group AS b ON a.id_user = b.id_user
SET a.date_end = a.date_begin
WHERE b.id_group = '14'
Assuming that 14 is a group id.
I am not that certain about its efficiency though. I still have to do more readings and learn more about it.
I want to get numOfItem from table BUY using ticketTypeId and then using the BUY.userId to find in the table USER to get the gender. So I can get numOfItem from table BUY and gender from table USER. I don't know how to write this in one query. Any idea?
table structure:
TABLE BUY:
ticketTypeId
numOfItem
userId
TABLE USER:
gender
You need to join your tables on a common field, in this case user id
Select b.ticketTypeId, b.numOfItem, b.userId, u.gender
From buy b inner join user u on b.userid = u.userid
Where b.ticketTypeId = <val>
You want to include where to get only needed ticketTypeId
Generally speaking a join between two tables is something like:
select table1.*,table2.*
from
table1
join table2 on table1.key=table2.key
Add userId in the table user
join the tables with inner join in the select statement
select a.*,b.* from [user] a inner join [buy] b on a.userid = b.userid
You need to use a join. Here is an link
SELECT tb1.ticketId, tb1.numOfItem, tb1.userId, tb2.gender
FROM Table1 as tb1
JOIN Table2 as tb2
ON tb1.userId = tb2.userId
Hi guys im working on a problem. i have 2 tables, table a contains a list of users table b contains a list of their "clock ins", at the moment im selecting distinct dates from table b and then for each date, im checking if the user has checked in for that date. BUT what i want to be able to do is select some data from the Table B as well .. but when i try i just get spammed with all the data from table B. here is my statement.
SELECT *
FROM Table A,
Table B
WHERE EXISTS (SELECT *
FROM Table B
WHERE Table B.user_id = Table A.id
And Table B.date = 'given date here')
I still don't understand why you want to select data that is not there, but if you still have to, then I would take your schema as-
USER
-----------------
user_id
username
...
...
USER_CLOCKIN
-----------------
user_id
clockin_datetime
...
...
I would query this as-
SELECT u.*, uc.*
FROM user u LEFT OUTER JOIN user_clockin uc
ON u.user_id = uc.user_id
WHERE uc.user_id IS NULL
;
This selects all the columns from both the tables. Modify your SELECT list as per your requirement.
Try running following:
SELECT table_a.name,table_b.date FROM table_a LEFT JOIN table_b ON table_a.id=table_b.id WHERE table_b.date=<xx.yy.zzzz>
of course replace xx.yy.zzzz with date wanted. also, visit http://www.mysql.org go to documentation, and find more info on join, left join and right join...
I feel like this is something I can search for, but I don't know the correct terminology to go about it.
I have a SQL database that has a a few tables. One table stores caller logs for a softphone (agent_log), one table stores campaign information for what the people on the softphone are calling on (campaigns). Both tables have the column "campaign_id" that I can use to call to each other (I think). I need to relate these two tables so that I can have a sql query that would look like
SELECT * FROM agent_log WHERE active = 'Y';
Obviously it doesn't work because the column 'active' doesn't exist in that table, it exists in the campaigns table. Is there any simple way to go about this?
What you are trying to do is called a JOIN You would JOIN the tables using a field that is common between them, so for your tables it would be campaign_id.
SELECT *
FROM agent_log al
INNER JOIN campaigns c
ON al.campaign_id = c.campaign_id
WHERE c.active = 'Y'
OR
SELECT *
FROM agent_log al
INNER JOIN campaigns c
ON al.campaign_id = c.campaign_id
AND c.active = 'Y'
I suggest you do some reading about JOINs. The Visual Explanation of Joins is a great start.
id campaign_id exists in both table and assuming that there is a relation between table you can join them:
SELECT agent_log.campaign_id
FROM agent_log, campaigns
WHERE agent_log.campaign_id = campaigns.campaign_id
AND campaigns.active = 'Y'
if you like to use JOIN i suggest you spend some of your time to learn it.
SELECT *
FROM agent_log
INNER JOIN campaigns ON agent_log.campaign_id = campaigns.campaign_id
WHERE campaigns.active = 'Y';
You simply need to use a JOIN query like this
SELECT al.*
FROM agent_log as al
INNER JOIN campaigns as c
ON al.campaign_id = c.campaign_id
WHERE c.active = 'Y';
You need to make sure you have indexed on campaign_id field on both tables and the active field in the campaign table to make the query run efficiently.
This should work without inner join
SELECT
agent_log.campaign_id
FROM
agent_log,
campaigns
WHERE
agent_log.campaign_id = campaigns.campaign_id
AND
campaigns.active = 'Y';