Question - let's say I have 2 tables.
Table 1 - name is permission_list, columns are ID (unique ID), col_ID, user_ID
Table 2 - name is list_entries, Columns are ID (unique ID), title, description, status
I want to select all the rows from table 2 that have status of 'public' as well as all the rows from table 2 that the ID from table 2 shows up in table 1 (under the column col_ID) AND if the user_ID in table 1 matches a certain value. So, anything public, or anything that this specific user has listed under the permissions table. This query would also remove duplicates - in case the user gets a public entry listed in their permissions_list, it wouldn't show up twice.
Hope that makes sense!
Here you go:
SELECT DISTINCT table2.* from table2
LEFT JOIN table1 USING (id)
WHERE status='public'
OR user_ID='someuser';
You need to get some education on JOIN for your first thing, and the second thing is called DISTINCT.
Start here... https://www.google.com/
You have not specified your join condition so we can't give you code samples really. Also the way you worded your question, I'm not entirely sure you don't want a UNION. Read up on those concepts and come back here when you can improve the question.
SELECT table_2.status, table_2.ID, table_1.col_ID
FROM table_1 JOIN table_2
WHERE table_2.status = 'public'
AND table_2.ID = table_1.col_ID
AND table_1.user_ID = 'certain value'
;
Try this
Related
I am trying to fetch data from the 2 tables using joins. But I don't know how to do it. I have read about joins but it wasn't making any sense since I am new.
I have two tables.
Playgrounds Inspection
id u_id id playground_id
1 2 1 1
2 2 2 2
In playgrounds table id is the playground id and is unique and u_id is the user of the playground.
In inspection table id is unique and playground_id is used as a foreign key from playgrounds table's id.
My problem is that I have to pass id and u_id to the the query in playgrounds table then it should select id from playground table. Then it should select every thing from inspection table based on that id .
Hope I have explained it properly.
Any help is much appreciated.
Thanks in advance
JOIN operations, whether INNER JOIN, LEFT JOIN, or RIGHT JOIN, have this overall syntax.
table_expression JOIN table_expression ON boolean_expression
When the Boolean expression is true, the JOIN concatenates the matching rows of the two tables into a single row in the result.
In your case, you want the ON clause's Boolean expression to say
Playgrounds.id = Inspection.playground_id
because that's how you know a row from Inspection relates to a row in Playgrounds. Accordingly, a statement like
SELECT Inspection.id AS Inspection_ID,
Playgrounds.id AS Playground_ID,
Playgrounds.u_id AS User_ID
FROM Playgrounds
JOIN Inspection ON Playgrounds.id = Inspection.playground_id
WHERE Playgrounds.u_id = something
AND Playgrounds.id = something_else
See how it goes? Your SELECT makes a new table, sometimes called a result set, by JOINing the rows of two existing tables on the criteria you choose in the ON clause.
You can put anything you want into the ON clause. You could put
Playgrounds.id = Inspection.playground_id OR Inspection.id < 20
or
Playgrounds.id = Inspection.playground_id OR Inspection.scope = 'citywide'
for example (if you had a scope column in the Inspection table).
But, don't go there until you master the basic JOIN operation.
first I will like to state that am still a newbie on writing SQL Queries. I thoroughly searched for an answer on this Error and I got a good number of answers, but none seems to be helpful or I will say I don't really know how to apply the solutions to mine.
Here is my challenge, I have an application table, that stores applicants records with some unique columns e.g (dl_number,parent_id,person_id). The parent_id keeps tracks of individual applicant history records with the his/her first record and each applicant is meant to have a unique dl_number, but for some reasons, some applicants dl_number(s) are not unique, hence a need to identify the records with changing dl_number(s).
Below is the SQL Query, that am getting the [sql error (1241) operand should contain 1 column(s)] error on.
SELECT id,application_id,dl_number,surname,firstname,othername,birth_date,status_id,expiry_date,person_id,COUNT(DISTINCT(dl_number,parent_id,birth_date)) AS NumOccurrences
FROM tbl_dl_application
WHERE status_id > 1
GROUP BY dl_number,parent_id,birth_date
HAVING NumOccurrences > 1
Please any help on how to solve this, or a better way to solve this.
Sample table and expected result
DISTICT is not really a function to be used that way.
You can do SELECT DISTICT column1, column2 FROM table to get unique rows only, or similarly SELECT column, count(DISTINCT anothercolumn) FROM table GROUP BY column to get unique rows within a group.
Problem as I understand it: You look for duplicates in your table. Duplicates are defined as having identical values of these 3 columns: dl_number, parent_id and birth_date.
I'm also assuming that id is a primary key in your table. If not, replace the t2.id <> t.id condition with one that uniquely identify your row.
If you only wanted to know what are the duplicated groups, this should work:
SELECT dl_number, parent_id, birth_date, count(*) as NumOccurences -- You can only add aggregation functions here, not another column unless you group by it.
FROM tbl_dl_application t
WHERE status_id > 1 -- I don't know what this is but it should do no harm.
GROUP BY dl_number, parent_id, birth_date
HAVING count(*)>1
If, however, you want to know details of each duplicated row, this query will give you that:
SELECT *
FROM tbl_dl_application t
WHERE
status_id > 1 -- I don't know what this is but it should do no harm.
AND EXISTS (
SELECT 1
FROM tbl_dl_application t2
WHERE
t2.dl_number = t.dl_number
AND t2.parent_id = t.parent_id
AND t2.birth_date = t.birth_date
AND t2.id <> t.id
)
ORDER BY dl_number, parent_id, birth_date, id; -- So you have your duplicates nicely next to each other.
Please explain further if I misunderstood your objective, or ask if the solution is not clear enough.
**You have to use only one column while use to DISTINCT function. You used this three field dl_number,parent_id,birth_date. Just use 1 filed from these 3. Then query will run.**
For example.
SELECT id,application_id,dl_number,surname,firstname,othername,birth_date,status_id,expiry_date,person_id,COUNT(DISTINCT(parent_id)) AS NumOccurrences
FROM tbl_dl_application
WHERE status_id > 1
GROUP BY dl_number,parent_id,birth_date
HAVING NumOccurrences > 1
I have a table of 'entries' in a MYSQL database. I have another table that records activity on those entries, with the id of the entry as a foreign key. I want to select from my first table entries that do not appear in the second table.
How can I use SQL to make this happen? Do I have to iterate through both tables and compare every entry with every other entry? Is there an easier way to do this?
ex. I have a table with an entry data column and a user name column. I have another table with an entry id column and a user id column. I want to select from my first table all of the entries which do not appear in the second table with a given user id.
Thanks ahead of time. I have been struggling with this experiment for a while. I imagine I have to join the two tables somehow?
Several ways to achieve this, NOT IN, NOT EXISTS, LEFT JOIN / NULL check. Here's one with NOT EXISTS:
SELECT *
FROM FirstTable T
WHERE NOT EXISTS (
SELECT *
FROM SecondTable T2
WHERE T.Id = T2.Id
)
From what I understand, you want to select all rows where the foreign key doesn't match anything in the other table. This should do the trick:
SELECT *
FROM Data A
RIGHT JOIN Entry B
ON A.ID = B.ID
WHERE A.ID IS NULL
Here's a handy chart that illustrates how to use joins for stuff like this.
You can also use NOT IN, and the mechanics for this one are actually a bit easier to understand.
SELECT *
FROM Data A
WHERE A.ID NOT IN (SELECT ID FROM Entry)
I have three tables that I want to combine.
I have the following query to run:
DROP TABLE
IF EXISTS testgiver.smart_curmonth_downs;
CREATE TABLE testgiver.smart_curmonth_downs
SELECT
ldap_karen.uid,
ldap_karen.supemail,
ldap_karen.regionname,
smart_curmonth_downs_raw.username,
smart_curmonth_downs_raw.email,
smart_curmonth_downs_raw.publisher,
smart_curmonth_downs_raw.itemtitle,
smart_items.`Owner`
FROM
smart_curmonth_downs_raw
INNER JOIN ldap_karen ON smart_curmonth_downs_raw.username = ldap_karen.uid
INNER JOIN smart_items ON smart_curmonth_downs_raw.itemtitle = smart_items.Title
I want to know how to create the joins while maintaining a one to one relationship at all times with rows in table smart_curmonth_downs_raw.
For instance if there is not a uid in ldap_karen I have issues. And then the last issue I have found is that our CMS is allowing for duplicate itemtitle. So if I run my query I am getting a lot more rows because it is creating a row for each itemtitle. For example would there be a way to only catch the last itemtitle that is in smart_items. I would just really like to maintain the same number of rows - and I have no control over the integrity issues of the other tables.
The smart_curmonth_downs_raw table is the raw download information (download stats), the karen table adds unique user information, and the smart_items table adds unique items (download) info. They are all important. If a user made a download but is knocked off the karen table I would like to see NULLs for the user info and if there is more than one item in smart_items that has the same name then I would like to see just the item with the highest ID.
It sounds like relationship between smart_curmonth_downs_raw and ldap_karen is optional, which means you want to use a LEFT JOIN which all the rows in the first table, and, if the right table does not exists, use NULL as the right table's column values.
In terms of the last item in the smart_items table, you could use this query.
SELECT title, MAX(id) AS max_id
FROM smart_items
GROUP BY title;
Combining that query with the other logic, try this query as a solution.
SELECT COALESCE(ldap_karen.uid, 'Unknown') AS uid,
COALESCE(ldap_karen.supemail, 'Unknown') AS supemail,
COALESCE(ldap_karen.regionname, 'Unknown') AS regionname,
smart_curmonth_downs_raw.username,
smart_curmonth_downs_raw.email,
smart_curmonth_downs_raw.publisher,
smart_curmonth_downs_raw.itemtitle,
smart_items.`Owner`
FROM smart_curmonth_downs_raw
INNER JOIN (SELECT title, MAX(id) AS max_id
FROM smart_items
GROUP BY title) AS most_recent
ON smart_curmonth_downs_raw.itemtitle = most_recent.Title;
INNER JOIN smart_items
ON most_recent.max_id = smart_items.id
LEFT JOIN ldap_karen
ON smart_curmonth_downs_raw.username = ldap_karen.uid;
This is my first time asking a question on here. It has been very helpful with learning.
I am trying to select a table and getting only rows that have a maximum value for its particular group in another table. One of the best answers that is very close but not quite there is this one (SQL Select only rows with Max Value on a Column) but it only relates to a single table. I have found some others with multiple table but not sure how exactly to use it.
I have a table with (simplified)
prodID, quantity, mach, etc
I then have a table with
prodStatusID, prodID, userID, subStatusID
a last table with sub status names
subStatusID, subStatusName
I am trying to get a table with all of the first table and the second table but only with the row that has the maximum status number and include the right status name.
My other concern which may not matter now but in a year or two when this thing starts to really fill up is performance. I dont know bad it is to have select inside a select but if I am trying to return all productions then it will be doing a query for every production.
Just to be clearer. in the second table prodStatus there might be 2 rows with prodID of 4 but the subStatusID for the first one would be 1 and the second one would be 2. The userID will be different. All I want to get back is the second row because it has the highest status number and I need the userID and statusName associated with that row.
I have been googling for 2 days to get this answer and I saw 1 about auctions but I just dont fully understand it even after researching it.
You need to create a subquery which get the maximum value of subStatusID for each prodID.
SELECT a.*, -- select only columns that you want to show
c.*, -- asterisks means all columns
d.*
FROM table1 a
INNER JOIN
(
SELECT prodID, max(subStatusID) maxID
FROM table2
GROUP BY prodID
) b ON a.prodID = b.prodID
INNER JOIN table2 c
ON b.prodID = c.prodID AND
b.maxID = c.subStatusID
INNER JOIN table3 d
ON c.subStatusID = d.subStatusID