i am trying to understand what does this query means? - mysql

select user.*,
store.storeId,store.storeName from
user JOIN store ON user.storeId=store.storeId
where email=? and password=?
This is my mysql query thought of when joining the table but i am not able to get what my query is doing i am unable to get that.

Select everything from the user table.
select user.*,
Select storeId and storeName from the store table.
store.storeId,store.storeName
join the 2 tables together where the storeId from user table is = to storeId in the store table where the email and pass = what they typed in the input
from
user JOIN store ON user.storeId=store.storeId
where email=? and password=?

Related

How to combine multiple queries into same table but different columns?

So I have two tables that i'm working with.
Users which consists of:
ID, NAME, CITY
Follow which consists of:
User(id), Follow(id)
I am trying to write a query that returns to me a table of the name of the user, the name of the follower's name and the followers city. I have this query written which returns the correct information, but prints it out 8 times for each row and I have no idea why.
Here is a link to my code
https://www.db-fiddle.com/f/aDPgZFknC1ybteWM6hwoFJ/3
FROM
(SELECT user.name
from follow, user
where follow.user = user.id) as NAME,
(Select user.name
from follow, user
where follow.follow = user.id) as FOLLOWER,
(select city
from user
right join follow
on user.id = follow.follow) AS CITY;
When joining tables it would be better to use the JOIN syntax. You can use as your base the follow table and transform it through the use of JOIN to display the information that you need. On the following example, you join follow with the user table two times to get the user and follower information.
SELECT U.name, U2.name, U2.city
FROM follow AS f
JOIN user AS U
ON f.user=U.id
JOIN user AS U2
ON f.follow=U2.id;

mysql write select subquery on same table with condition on the resultset

I want to get all the data from users table with createdBy populated with name matching userid.
means
instead of showing createdBy column with id, I want to show createdBy
name querying by userid. Is there any possibility. Please help me.
table: User
This is what i get
This is what i need
Use self join
demo
select a.*,b.name as createdbyname from user a left join user b
on b.id=a.createdby
This should do the trick. You join the table with itself (actually, with only the userid and names fields, to avoid conflicts with the original createdBy field) on the matching userids, then select only the relevant fields
SELECT u.userId, u.name, sj.createdBy
FROM users u JOIN (SELECT userId, name AS createdBy FROM users) AS sj
ON u.createdBy = sj.userId

Add to VIEW from another table

I need to find out how to pull user names into a VIEW from a different table. I have 3 tables, User, Lead, and Lead_detail. In the users table there is an ID field which is stored in the Created_By field in the Lead table. In the Lead table I have an ID field that is stored in the Lead_detail Lead_ID field.
I have created a VIEW to the Lead_detail table which pulls all the info I need but I have found that I don't have the users name in that VIEW so I need to ALTER my view to add in the users names per lead but I am having trouble with the statement.
Before altering the VIEW I wanted to try a SELECT statement to see if I get any data,
SELECT * FROM Lead_detail
JOIN Lead
ON Lead_detail.lead_id = Lead.id
WHERE Lead.Created_by = Users.ID
But this didn't work. What would be a correct statement so that I can pull the users names into the Lead VIEW?
I think you missed a join to the Users table:
SELECT
*
FROM
Lead_detail
INNER JOIN Lead
ON Lead_detail.lead_id = Lead.id
INNER JOIN Users
ON Lead.Created_by = Users.ID

mysql inner join interrogate 3 tables

I have a table in a MySQL DB, called ‘users’. The fields for users are : id, email, username, first_name, last_name. Another table in the same MySQL DB, called ‘premium_users’ , contains 2 fields : user_id, premium_service_id. A third table called ‘premium_services’ contains 2 fields : premium_service_id , premium_service_name.
I want to create an SQL query , to interrogate my db, so i can have a full list of what premium services has every premium user. How can i interrogate properly with inner join? I’ve try this :
select * from users inner join premium_users on users.id = premium_users.user_id inner join premium_services on premium_users.premium_service_id = premium_services.premium_service_id;
Since you say which service has every user, you'll need to use aggregation to determine this. Here's one way:
select user_id
from premium_users
group by user_id
having count(*) = (select count(*) from premium_services)
SQL Fiddle Demo
Depending on your data, you may need count(distinct premium_service_id) instead, but you should have constraints that don't allow duplicates in those table.
Rereading your question, I might have got this backwards. Looks like you want a list of premium services instead of users. Same concept applies:
select ps.premium_service_id
from premium_services ps
join premium_users pu on ps.premium_service_id = pu.premium_service_id
group by ps.premium_service_id
having count(distinct pu.user_id) = (select count(distinct user_Id) from premium_users)
More Fiddle

MySQL query summing records for users

I'm using MySQL with phpmyadmin -- which I only started to use today. If y'all can help me with this query you will create some major happiness:
My objective: to identify which users of status "userEnabled" have >3 records from the goals table associated with them.
userID is a field that relates the tables.
TABLE NAMES: users, goals
I think this would be the beginning of the query:
SELECT * FROM `users` WHERE `userEnabled`=1
Please let me know any details needed.
You need to join record from goals where the userid matches. Filter on the enabled flag, then count the results. Something like:
select * from users
INNER JOIN goals ON users.userID = goals.userID
WHERE user.userEnabled = 1
GROUP BY user.userID
HAVING count(user.userID) > 3