SQL SELECT QUERY - mysql

i have 3 tables:
users:
uid int(11) - userid(primary key, auto_increment)
name varchar(255)
pass varchar(64)
created int(11)
projects:
pid int(11) .....
name varchar(150)
description varchar(255)
created int(11)
users_projects:
uid int(11) - user id
pid int(11) - product id
What is the query for: showing the projects(name and description) sorted by name for the user with a certain name?
This is what i've got so far:
SEECT name,description FROM projects ORDER BY name ASC

SELECT a.*, b.*
FROM users a
INNER JOIN users_projects b
ON a.uid = b.uid
INNER JOIN projects c
ON b.pid = c.pid
ORDER BY a.Name ASC
To further gain more knowledge about joins, kindly visit the link below:
Visual Representation of SQL Joins

Looks like you just need to join up the tables. Here #Name is a parameter containing the user's name you want project details for:
SELECT projects.Name,
projects.Description
FROM projects
INNER JOIN users_projects
ON projects.pid = users_projects.pid
INNER JOIN Users
ON users_projects.UId = Users.UId
WHERE Users.Name = #Name
ORDER BY projects.Name ASC

Related

Using IN operator with a subquery

Having the following scheme
CREATE TABLE request (
`id` int,
`classroom` varchar(255),
`school` varchar(255),
`date_of_application` datetime,
`status` varchar(100),
`user_id` bigint(20) NOT NULL
);
CREATE TABLE user (
`id` int,
`full_name` varchar(255)
);
CREATE TABLE user_schools (
`user_id` int,
`schools_string` varchar(255)
);
I need to obtain a list of requests where the status field is equal to 'pending' and where the school field is located in the list of schools associated with a user
Right now i am trying the following query
SELECT
r.id,
u.full_name,
r.date_of_application,
r.classroom
FROM
request r
inner join
user u on u.id = r.user_id
WHERE
r.school IN (
SELECT
us.schools_string
FROM
user_schools us
WHERE
us.user_id = u.id
)
AND r.status = 'pending';
I currently get the total list of requests. I understand what is failing is the subquery.
Attached a sqlfiddle to facilitate your collaboration
The result I expect are three applications where applicants are: user user, user user and another assistant user
Thanks for any idea. If there is a better solution, I thank you.
Regards
The code is doing what you are asking. If you want user/requests that are in the list, then you can use select distinct like this:
SELECT DISTINCT u.full_name, r.date_of_application
FROM request r inner join
user u
on u.id = r.user_id
WHERE r.school IN (SELECT us.schools_string
FROM user_schools us
WHERE us.user_id = u.id
) AND
r.status = 'pending';
But if you want the classroom and request id, then you are going to get your original result set (or some variation).

How to Select data trough 2 tables with ONE Query

Every client has their own project but not all of them has project.
I want to select all clients that has project but I do not know how to do it!
I get all clients with this query:
SELECT `clients` FROM `reps` WHERE `clients` != ''
My goal is to get data which has only project
These are my database tables:
Table Clients: (Table name = reps)
1 id varchar(12) // example: stckvrflw
2 ctitle varchar(100) // example: StackOverflow
Table Projects: (Table name = verkocht)
1 id varchar(11) // example: 1
2 title varchar(100) // example: This is an Example
Do you have a solution on my problem?
You have the client in the milestones table, so doesn't this do what you want?
SELECT DISTINCT m.client
FROM `milestones` m;
You have to join the projects and the milestones tables. Since you gave no information on your database shema, the field names have to be adjusted. You also have to check for deleted or hidden flags if you have some:
SELECT c.* FROM clients c
INNER JOIN projects p ON p.client = c.id
INNER JOIN milestones m ON m.project = p.id
GROUP BY c.id

Get data from 3 table

I have three tables in my database first one is account_group, next is ledger and last one is account_receipt,
account_group has fields group_id and group_name, and this group_id is mapped to ledger, ledger table contains fields ledger_name and group_id. and in the last table account_receipt has fields ledger_id and receipt_amount.
so what I need is I want to get receipt details when group_id is given.I cant write the query for this.any help would be appreciated.
schema::
account_group::
id int(11)
group_name varchar(60)
ledger ::
id int(11)
group_id varchar(60)
ledger_name varchar(60)
account_receipt::
id int(11)
ledger_id int(11)
amount float
receipt_date date
Try the following query. It uses JOINs to connect the various tables, column aliases and table aliases.
SELECT
ag.id group_id,
l.id ledger_id,
l.ledger_name ledger_name
ar.id receipt_id,
ar.amount amount,
ar.receipt_date receipt_date
FROM account_receipt ar
INNER JOIN ledger l ON ar.ledger_id = l.id
INNER JOIN account_group ag ON ag.id = l.group_id AND ag.id = <given group id>;
Three table joining example has been given below
select * from Table_A A
join Table_B B on B.id=A.id
join Table_C C on C.id=A.id

Alternative to GROUP_CONCAT? Multiple joins to same table, different columns

I'm not even sure of the correct terminology here. MySQL newbie, more or less.
Given a couple tables defined as follows:
CREATE TABLE users
( user_id int(11) NOT NULL auto_increment
, name VARCHAR(255)
, pri_location_id mediumint(8)
, sec_location_id mediumint(8)
, PRIMARY KEY (user_id)
);
CREATE TABLE locations
( location_id mediumint(8) NOT NULL AUTO_INCREMENT
, name varchar(255)
, PRIMARY KEY (location_id)
)
I'm trying to do a query to get the user name and both primary and secondary locations in one go.
I can get one like this:
SELECT u.name AS user_name, l.name as primary_location FROM users u, locations l WHERE u.primary_location_id=l.location_id
But I'm drawing a total blank on the correct syntax to use to get both in one query.
SELECT u.name AS user_name, l1.name as primary_location , l2.name as secondary_location
FROM users u
JOIN locations l1 ON(u.pri_location_id=l1.location_id)
JOIN locations l2 ON(u.sec_location_id = l2.location_id);
First of, I would strongly consider changing your DB schema if allowable to add a users_locations table that can be used to properly describe this many to many relationship.
This table could look like:
user_id location_id location_type
1 1 primary
1 2 secondary
2 1 secondary
2 2 primary
and so forth.
You would likely want a compound primary key across all three columns. And location_type might best be enum data type.
Your query would then be like
SELECT
u.name AS user_name
l.name AS location_name
ul.location_type AS location_type
FROM users AS u
INNER JOIN user_location AS ul /* possibly use left join here if user can exist without a location */
ON u.user_id = ul.user_id
INNER JOIN locations AS l
ON ul.location_id = l.location_id
ORDER BY ul.location_type ASC
This would return up to two records per user (separate record for primary and secondary, primary listed first)
If you need this collapsed to a single record you could do this:
SELECT
u.name AS user_name
COALESCE(CASE WHEN ul.location_type = 'primary' THEN l.name ELSE NULL END CASE) AS primary_location,
COALESCE(CASE WHEN ul.location_type = 'secondary' THEN l.name ELSE NULL END CASE) AS secondary_location
FROM users AS u
INNER JOIN user_location AS ul /* possibly use left join here if user can exist without a location */
ON u.user_id = ul.user_id
INNER JOIN locations AS l
ON ul.location_id = l.location_id
GROUP BY `user_name`
If however you are stuck with current schema, then solution by #Jlil should work for you.

Mysql table scheme for featured / emphasized products

Well my problem is that I would like to store some kind of products in their own tables and from these products I would like to select some into a featured / emphasised products table which would store the ID of the selected rows from each table to display those featured products on the home page of a website.
After selecting the featured products from their tables I would like to make my own order how they will be listed, so I think a new table is needed where I can store the ID and the order of the featured products, but I can't imagine how to connect these tables properly.
I was thinking a lot how to solve this problem but, I hope somebody will know the correct answer!
Create a second table:
CREATE TABLE FEATURED_PRODUCTS (
ID INTEGER NOT NULL,
PRODUCT_ID INTEGER NOT NULL,
PRODUCT_ORDER INTEGER NOT NULL
)
Then just join with your original table when you want to find your featured products:
SELECT P.*
FROM PRODUCTS P
INNER JOIN FEATURED_PRODUCTS FP ON P.ID = FP.PRODUCT_ID
ORDER BY FP.PRODUCT_ORDER
Its probably worth stating that ID in the FEATURED_PRODUCTS table is not strictly necessary, I just don't like having tables without primary key columns.
---- EDIT ----
More complete example:
CREATE TABLE FRUIT_PRODUCTS (
ID INTEGER NOT NULL,
NAME VARCHAR(255),
PRICE INTEGER NOT NULL,
FARM_OF_ORIGIN VARCHAR(255)
)
CREATE TABLE BREAD_PRODUCTS (
ID INTEGER NOT NULL,
NAME VARCHAR(255),
PRICE INTEGER NOT NULL,
TYPE_OF_GRAIN VARCHAR(255)
)
CREATE TABLE MEAT_PRODUCTS (
ID INTEGER NOT NULL,
NAME VARCHAR(255),
PRICE INTEGER NOT NULL,
ANIMAL VARCHAR(255)
)
CREATE TABLE FEATURED_PRODUCTS (
ID INTEGER NOT NULL,
TABLE_NAME VARCHAR(255),
PRODUCT_ID INTEGER NOT NULL,
PRODUCT_ORDER INTEGER NOT NULL
)
Then you could join them all thusly:
SELECT FP.TABLE_NAME, P.ID, P.NAME, P.PRICE, P.FARM_OF_ORIGIN,
NULL AS TYPE_OF_GRAIN, NULL AS ANIMAL
FROM FEATURED_PRODUCTS FP
INNER JOIN FRUIT_PRODUCTS P ON FP.TABLE_NAME = 'FRUIT_PRODUCTS'
AND FP.PRODUCT_ID = P.ID
UNION
SELECT FP.TABLE_NAME, P.ID, P.NAME, P.PRICE, NULL AS FARM_OF_ORIGIN,
P.TYPE_OF_GRAIN, NULL AS ANIMAL
FROM FEATURED_PRODUCTS FP
INNER JOIN BREAD_PRODUCTS P ON FP.TABLE_NAME = 'BREAD_PRODUCTS'
AND FP.PRODUCT_ID = P.ID
UNION
SELECT FP.TABLE_NAME, P.ID, P.NAME, P.PRICE, NULL AS FARM_OF_ORIGIN,
NULL AS TYPE_OF_GRAIN, P.ANIMAL
FROM FEATURED_PRODUCTS FP
INNER JOIN MEAT_PRODUCTS P ON FP.TABLE_NAME = 'MEAT_PRODUCTS'
AND FP.PRODUCT_ID = P.ID
Which would give you a result set containing all the featured products. Note that this is untested, but should get the idea across.