How do I achieve the following using JOIN statement in mysql? - mysql

I have
TABLE 1: r_profile_token
Columns:
r_sequence int(45) AI PK
r_profileid varchar(45)
r_token varchar(300)
r_deviceType int(1)
r_date date
r_time time
and
TABLE 2: r_token_arn
Columns:
r_token varchar(300) PK
r_arn varchar(300)
I need a result of the form -
r_profileid
r_arn
r_deviceType
where I can specify the r_profileid.
So far my SQL statement is:
SELECT
b.r_arn,
a.r_deviceType
FROM
coffee2.r_profile_token a INNER JOIN
coffee2.r_token_arn b
ON a.r_token=b.r_token;
Which returns r_arn and r_deviceType but for all r_profileid?
How do I modify the statement so that it returns me r_arn and r_deviceType only corresponding to a specific r_profileid?

Use a WHERE clause.
SELECT B.R_ARN, A.R_DEVICETYPE
FROM COFFEE2.R_PROFILE_TOKEN A
INNER JOIN
COFFEE2.R_TOKEN_ARN B
ON A.R_TOKEN=B.R_TOKEN
WHERE R_PROFILEID = 'SOME_VALUE';
If you want for a single profileid, then use
WHERE R_PROFILEID = 'SOME_VALUE';
If you want for a range of profileIds , then use
WHERE R_PROFILE_ID IN ('VALUE1','VALUE2','VALUE3');

You can try this Query against your requirements.
SELECT
b.r_arn,
a.r_deviceType ,
a.r_profileid
FROM
r_profile_token a
INNER JOIN
r_token_arn b
ON
a.r_token=b.r_token
where r_profileid='profile name';

You need to put a where condition in your MYSql query.
select b.r_arn, a.r_deviceType from coffee2.r_profile_token a
INNER JOIN coffee2.r_token_arn b on a.r_token=b.r_token
where r_profileid = "Specific value";

select b.r_arn, a.r_deviceType, a.r_profileid from r_profile_token a
INNER JOIN r_token_arn b on
a.r_token=b.r_token;

Related

Error Code: 1054. Unknown column in 'on clause'

The query is meant to return the percentage based on value from one table being divided by the value of another table. However, there is something wrong and I am missing it.
similar problems noted on the board looked related to JOIN, but did not seem to be the problem, when I tried and explicit join -- basically mysql was like -- now you are an idiot-- I must have did that wrong or that is not the problem.
SELECT (pathogenPop / locationpop) as PercentInfected
FROM (
(SELECT apinfectcount.APInfectCountInfected
as pathogenPop, apinfectcount.APInfectCountLocation
FROM apstart.apinfectcount
GROUP BY apinfectcount.APInfectCountLocation) as pathogenPop
Inner JOIN
(SELECT apcountrypop.apcountrypopPopulation
as locationpop, apcountrypop.apcountrypopCountry
FROM apstart.apcountrypop
GROUP BY apcountrypop.apcountrypopCountry)
as locationpop
on apinfectcount.APInfectCountLocation = apcountrypop.apcountrypopCountry
and apinfectcount.APInfectCountWeek = 23);
Table Schema: apcountrypop
idapcountrypop INT(11)
apcountrypopCountry VarChar(45)
apcountrypopPopulation FLOAT
Table Schema: apinfectcount
idAPInfectCount INT(11)
APInfectCountLocation VarChar(45)
APInfectCountOutBreak VarChar(45)
APInfectCountPathogen VarChar(45)
APInfectCountInfected FLOAT
APInfectCountDead FLOAT
APInfectCountWeek VarChar(45)
If it worked --
it would assign apinfectcount.APInfectCountInfected to pathogenPop
and apcountrypop.apcountrypopPopulation to locationpop
for the values where the locations are the same(apinfectcount.APInfectCountLocation = apcountrypop.apcountrypopCountry)
then it would return the value of the apinfectcount table value is divided by the apcountrypop table to give the percentage.
so in this specific example I only have sample data so I am just wanted to return one value so I added the where clause to just test the logic and syntax.
I appreciate the help.
You have assugned the tables alias pathogenPop and locationpop so
you need pathogenPop.APInfectCountLocation = locationpop.apcountrypopCountry
and pathogenPop.APInfectCountWeek = 23 in ON clause
SELECT (pathogenPop / locationpop) as PercentInfected
FROM (
(SELECT apinfectcount.APInfectCountInfected
as pathogenPop, apinfectcount.APInfectCountLocation
FROM apstart.apinfectcount
GROUP BY apinfectcount.APInfectCountLocation) as pathogenPop
Inner JOIN
(SELECT apcountrypop.apcountrypopPopulation
as locationpop, apcountrypop.apcountrypopCountry
FROM apstart.apcountrypop
GROUP BY apcountrypop.apcountrypopCountry)
as locationpop
on pathogenPop.APInfectCountLocation = locationpop.apcountrypopCountry
and pathogenPop.APInfectCountWeek = 23) T;
and also a table alias for the outer FROM(..) T
I don't have the database to test against so I'm not 100% certain this will run, but would the following query not be a bit simpler?
SELECT (apinfectcount.APInfectCountInfected / apcountrypop.apcountrypopPopulation) as PercentInfected, apinfectcount.APInfectCountLocation
FROM apinfectcount
INNER JOIN apcountrypop ON apcountrypop.apcountrypopCountry = apinfectcount.APInfectCountLocation
WHERE apinfectcount.APInfectCountWeek = 23
GROUP BY apinfectcount.APInfectCountLocation
And I assume there is only one location record per location in each table?
There is an issue within a query. As scope of apinfectcount.APInfectCountLocation column and apcountrypop.apcountrypopCountry column is limited to subquery only you cannot use it outside the subquery (within where clause).
You can check out these docs on subquery https://learn.microsoft.com/en-us/sql/relational-databases/performance/subqueries?view=sql-server-2017
Refer code below.
SELECT (countInfected / countrypopulation) as PercentInfected
FROM (
(SELECT apinfectcount.APInfectCountInfected
as countinfected, apinfectcount.APInfectCountLocation, APInfectCountWeek as
countweek
FROM apstart.apinfectcount
GROUP BY apinfectcount.APInfectCountLocation) as pathogenPop
Inner JOIN
(SELECT apcountrypop.apcountrypopPopulation
as countrypopulation, apcountrypop.apcountrypopCountry
FROM apstart.apcountrypop
GROUP BY apcountrypop.apcountrypopCountry)
as locationpop
on pathogenPop.countinfected = locationpop.countrypopulation
and pathogenPop.countweek= 23);

How to get values through connected tables?

I have such a question. I got two tables, the first one contains comments, and the second id comments and album id to which the comment was left
> CREATE TABLE `review` (`id` VARCHAR(32) NOT NULL,
> `user_id` VARCHAR(32) NOT NULL,`comment` MEDIUMTEXT NOT NULL,
> PRIMARY KEY (`id`) )
> CREATE TABLE `review_album` (`review_id` VARCHAR(32) NOT NULL,
> `album_id` VARCHAR(32) NOT NULL, PRIMARY KEY (`review_id`,
> `album_id`), INDEX `review_album_review_idx` (`review_id`) )
I tried this way:
SELECT * from review_album JOIN review WHERE album_id = '300001'
But i got result two times.
How can I get comment text for a specific album_id?
The general syntax is:
SELECT column-names
FROM table-name1 JOIN table-name2
ON column-name1 = column-name2
WHERE condition
The general syntax with INNER is:
SELECT column-names
FROM table-name1 INNER JOIN table-name2
ON column-name1 = column-name2
WHERE condition
Note: The INNER keyword is optional: it is the default as well as the most commonly used JOIN operation.
Refrence : https://www.dofactory.com/sql/join
Try with InnerJoin
SELECT *
FROM review_album
JOIN review ON review_album.review_id=review.id
WHERE album_id = '300001'
Reference
you have forgotten the on condition, everytime you have a join you'd better specify the condition of join, otherwais you have every connection available.
Hovewer the solution
SELECT *
FROM review_album RA
JOIN review R ON RA.column_fk = R.column_fk
WHERE album_id = '300001'
Here the documentation for join https://www.w3schools.com/sql/sql_join.asp
try using this :
SELECT *
FROM review_album ra
JOIN review r ON rareview_id=r.id
WHERE album_id = '300001'

SQL column name ambiguous

I am having trouble with a new IMDB like system I'm building. My specific issue is that when I run:
CREATE VIEW `directors` AS
SELECT
`stars`.`id` AS `movie_id`,
`stars`.`title`,`stars`.`rating`,
`stars`.`storyline`,
`stars`.`star`,
`people_list`.`name` AS `director`
FROM `stars`
INNER JOIN `stars`
ON `movie_directors`.`movie` = `stars`.`id`
INNER JOIN `people_list`
ON `movie_directors`.`director` = `people_list`.`id`
WHERE `movie_directors`.`enabled` = 1;
I get the following error:
#1052 - Column 'stars.id' in field list is ambiguous
All of the questions I've found on here seem to relate to when you don't prefix the column name with a table name or, in this case, a view name since I'm writing a view to build off another view
You are selecting from stars and then INNER JOINing on stars:
SELECT ... FROM stars INNER JOIN stars
I think that you probably want to join with movie_directors based on your query.
You are using a self join (starts table is used two time) in this case you need an alias for refer the proper table instance
CREATE VIEW `directors` AS
SELECT
`stars`.`id` AS `movie_id`
, `stars`.`title`
,`stars`.`rating`
, `stars`.`storyline`
, `stars`.`star`
, `people_list`.`name` AS `director`
FROM `stars`
INNER JOIN `stars` as s2 ON `movie_directors`.`movie` = s2.`id`
INNER JOIN `people_list` ON `movie_directors`.`director` = `people_list`.`id`
WHERE `movie_directors`.`enabled` = 1;
I think it's because you are not aliasing correctly.
It should be something like
select ...
from stars
inner join stars as anothername
It looks to be ambiguous because you have two references to the stars table. Your FROM clause and your first INNER JOIN.
It looks like you are intending to join on movie_directors instead of INNER JOIN stars clause. E.g.
CREATE VIEW `directors` AS SELECT
`stars`.`id` AS `movie_id`,
`stars`.`title`,`stars`.`rating`, `stars`.`storyline`, `stars`.`star`, `people_list`.`name` AS `director`
FROM `stars`
INNER JOIN `movie_directors` ON `movie_directors`.`movie` = `stars`.`id`
INNER JOIN `people_list` ON `movie_directors`.`director` = `people_list`.`id` WHERE `movie_directors`.`enabled` = 1;
Hope this helps!

how to grab the not equal row from joined table in mysql?

i have two tables as following
user_job_applied
company_viewed_user
i want to take rows which are not in company_viewed_user table for relevant job_id in user_job_applied table.
i wrote the following query for it but it's not grabbed
SELECT
`user_job_applied`.user_id
FROM
`user_job_applied`
LEFT JOIN
`company_viewed_user`
ON
`company_viewed_user`.user_id ON `company_viewed_user`.user_id = `user_job_applied`.user_id
WHERE
`company_viewed_user`.user_id IS NULL AND
`company_viewed_user`.emp_id IN (SELECT user_id
FROM company_user
WHERE company_id='1')
AND
`user_job_applied`.job_id = '1';
The output should be as following
want to move emp_id condition to left join statement from in where clause
SELECT
`user_job_applied`.user_id
FROM
`user_job_applied`
LEFT JOIN `company_viewed_user` ON `company_viewed_user`.user_id = `user_job_applied`.user_id AND
`company_viewed_user`.emp_id IN (SELECT user_id FROM company_user WHERE company_id='1')
WHERE
`company_viewed_user`.user_id IS NULL AND
`user_job_applied`.job_id = '1';
You can use below query
SELECT uja.user_id
FROM `user_job_applied` AS uja
LEFT JOIN `company_viewed_user` AS cvu ON cvu.user_id = uja.user_id
WHERE cvu.user_id IS NULL;
If you want to add any other filter then you can include your own or let me know.
Added new conditions
SELECT uja.user_id
FROM `user_job_applied` AS uja
JOIN company_user AS cu ON cu.user_id=uja.emp_id
LEFT JOIN `company_viewed_user` AS cvu ON cvu.user_id = uja.user_id
WHERE uja.job_id=1 AND cu.company_id=1 AND cvu.user_id IS NULL;
Note: If job_id and company_id is not Int type then enclosed in quotes.

MySQL query to select rows where one column matches and one column does not

Here are my table setups
Results_class
CREATE TABLE appfilter.results_class (
activity varchar(120) NOT NULL,
class varchar(255) NOT NULL,
PRIMARY KEY (activity, class)
)
Users
CREATE TABLE appfilter.user (
user varchar(15) NOT NULL,
name varchar(100) NOT NULL,
activity_full varchar(200) NOT NULL,
activity varchar(125) NOT NULL,
class varchar(125) NOT NULL
)
The results i want is simple. I want to see all rows where activity matches and class does not.
Example
Results_class
activity class
com.google.android.apps.plus com.google.android.apps.circles.realtimechat.ConversationListActivity
com.google.android.apps.plus com.google.android.apps.plus.phone.ConversationListActivity
Users
activity class
com.google.android.apps.plus com.google.android.apps.circles.realtimechat.ConversationListActivity
I need it to select this missing activity/class which is
activity class
com.google.android.apps.plus com.google.android.apps.plus.phone.ConversationListActivity<br>
I have tried this and it gives me all the activity/classes. Now if i can just remove the ones already in users thats what i need.
SELECT
rc.activity, rc.class
FROM results_class rc
INNER JOIN user b
ON b.activity = rc.activity
Try this .. The below query emulates the MINUS operation ie matches all in class a which are not in b.
SELECT a.activity,a.class FROM results_class a
left join user b on (a.activity = b.activity and a.class = b.class)
where b.class is null
Working fiddle
AFTER EDIT
considering the missed information in the question
"If there is a activity in results_class and not in users its bringing it back too. I only need matching activities to bring non matching classes"
select a1.activity,a1.class from (select a.activity,a.class FROM results_class a
left join user b on (a.activity = b.activity and a.class = b.class)
where b.class is null) a1 inner join user b1 on a1.activity = b1.activity
Try using a subquery with exists
SELECT rc.activity, rc.class
FROM results_class rc
JOIN users c on c.activity = rc.activity
WHERE NOT EXISTS
( SELECT 1
FROM user b
WHERE b.class = rc.class
)
So basically you get all of the results_class activities when joined to users.. then filter out the classes that are in results_class but not in users.
WORKING FIDDLE