I have normalized tables I want to select the items that belong to the userid
I'm familiar with select syntax but I'm very weak in joins tables so I'm a bit confused on how to get the items that belong to the user should I use join ? or is there other way
this is just simple example of my tables they have more fields
..........
user
..........
userid
firstname
address
..........
items
..........
itemsid
itemName
itemDescription
..........
user_items
..........
userid(FK)
itemsid(FK)
Use two inner join
select a.*, b.*
from user_items as c
inner join user as a on a.userid = c.userid
inner join items as b on b.itemsid = c.itemsid;
Use INNER JOIN
SQL
select user.*, items.*
from user_items
inner join user on user.userid = items.userid
inner join items on items.itemsid = user_items.itemsid;
So if I read this correctly, user_items.userid = user.userid.
So you want to join, something like this.
SELECT i.itemsid, i.itemName, i.Description FROM items i JOIN users us ON ui.userid = us.userid JOIN user_items ui ON ui.itemsid = i.itemsid WHERE ui.userid = VALUE;
Replace Value with your actuall user id
Related
I have fours tables and I wanted to join all three tables with the one table.
I have listed my problem as follows:
Tables:
users
user_training
user_courses
user_certificates
I wanted to get the data from [2,3,4] tables that user_id field matches with the users table ID field.
When I try the INNER JOIN it gives me the result for users that are common in all the tables, But I just wanted to check the [2,3,4] tables with the table [1] Records.
My Query...
SELECT A.training_name AS 'training_name', C.course_name AS 'course_name', D.certificate_name AS 'certificate_name'
FROM user_training AS A INNER JOIN users AS B ON A.user_id=B.ID INNER JOIN user_courses AS C ON B.ID = C.user_id INNER JOIN user_certificates AS D ON B.ID = D.user_id;
Thanks in Advance.
use left join
select u.* from users u
left join user_training ut on ut.user_id=u.user_id
left join user_courses uc on uc.user_id=u.user_id
left join user_certificates uct on uct.user_id=u.user_id
With this one you are getting all users and their respective trainings:
SELECT *
FROM `users`
LEFT JOIN `user_training` ON `users`.`id` = `user_training`.`user_id`
Changing *_trainig to *_courses or *_certificates will return all users with respected courses or certificates.
If you need to get data in one query, try this one:
SELECT *
FROM `users`
LEFT JOIN `user_training` ON `users`.`id` = `user_training`.`user_id`
LEFT JOIN `user_courses` ON `users`.`id` = `user_courses`.`user_id`
LEFT JOIN `user_certificates` ON `users`.`id` = `user_certificates`.`user_id`
If user has no trainings, courses, certificates all remaining fields will be null-ed.
I have 3 tables:
-Sales
-Items_cstm
-Items
Sales and Items_cstm contains the data I have to get with the query and Items the ability of the item "Deleted (1 or 0)" (and dome more info I don't need).
The Items_cstm's id is = Items's id.
I have to list the Sales of the Items which aren't deleted (0).
I've tried somehow with inner join but it didn't work and I don't really know what am I doing wrong:
SELECT Items_cstm.quantity
FROM Items_cstm, Sales
WHERE '".$_POST['name']."' = Sales.name
AND
INNER JOIN Items ON Items_cstm.id = Items.id
WHERE Items.deleted = 0;
You can join your tables like this (but you did not show the link between Items_cstm and sales - you have to modify that)
SELECT ic.quantity
FROM Items_cstm ic
INNER JOIN Sales s on s.id = ic.id
INNER JOIN Items i ON ic.id = i.id
WHERE i.deleted = 0
AND s.name = '".$escapedName."'
Also always escape your user input.
Try something like this:
SELECT Items_cstm.quantity
FROM Items_cstm
INNER JOIN Sales ON sales.JOINCOLUMN = Items_cstm.JOINCOLUMN
INNER JOIN Items ON Items_cstm.id = Items.id
WHERE Sales.name = '".$_POST['name']."'
WHERE Items.deleted = 0;
Replace JOINCOLUMN above with the columns that let you match records between these two tables
Something like this should work:
SELECT Items_cstm.quantity
FROM Items_cstm
INNER JOIN Items ON Items_cstm.id = Items.id
INNER JOIN Sales ON Items_cstm.id = Sales.item_id // ??? check this one for column names
WHERE '$name' = Sales.name AND Items.deleted = 0;
Beware of SQL injection.
I have the following query which works perfectly:
SELECT *
FROM contacts
WHERE id in (
SELECT DISTINCT contacts.id
FROM contacts
INNER JOIN contacts2tags
ON contacts.id = contacts2tags.contactid
WHERE tagid in(7,4)
)
Here contacts table contains id, first_name, last_name, ..and tags table contains id, name. contacts2tags table contains contactid and tagid which are same as contacts.id and tags.id respectively
Now, what I want is, to display only the contacts which have both a tagid 7 and a tagid 4.
I tried something like this:
SELECT *
FROM contacts
WHERE id IN
(
SELECT CT1.contactid
FROM
tags T1, contacts2tags CT1, tags T2, contacts2tags CT2
WHERE CT1.contactid = CT2.contactid
AND CT1.tagid = T1.id
AND CT2.tagid = T2.id
AND (T1.id = 7 AND T2.id = 4)
and it works too.
My problem is, I want to convert the above second query to one using inner joins.
I have an array of ids stored in $tmp in php
I want to use those ids and write the above query for them.
How do I do that? I am not comfortable with sql. Might be its a very simple thing to ask.
Thanks in advance
EDIT:
The answer below solved the problem. But the sql runs very slow for 10k records. Any suggestions to optimise it? Pasting the updated query as given in the answer.
SELECT c.id
FROM contacts c
inner join contacts2tags t on c.id = t.contactid
where t.tagid in (7,4)
group by c.id
having count(distinct t.tagid) = 2
This should work
SELECT c.id
FROM contacts c
inner join contacts2tags t on c.id = t.contactid
where t.tagid in (7,4)
group by c.id
having count(distinct t.tagid) = 2
I want to select from 2 different tables.
In the first table I want to select all, but I will display only what I want.
In the second table I want to select only the profile picture but even if a user does not have a profile picture his data from the user table should be selected.
I am using inner joins. Below is my code:
SELECT * FROM
tish_user INNER JOIN tish_images
ON tish_user.user_id = tish_images.user_id
WHERE tish_images.prof_image = 1
To select from two different tables, you should specify values from each table that you want, not using catch-all *. Using a LEFT JOIN instead of an INNER JOIN lets you connect the tables you are querying from on a single point. You can query any kind of relationship between the tables at that point.
This query will give you all the userids in tish_user returning the matching tish_images.prof_image record if prof_image is 1, NULL otherwise.
SELECT
tish_user.user_id,
tish_images.prof_image
FROM
tish_user
LEFT JOIN tish_images
ON tish_user.user_id = tish_images.user_id
AND tish_images.prof_image = 1
Try this way
SELECT * FROM
tish_user, tish_images
WHERE tish_user.user_id = tish_images.user_id
AND
tish_images.prof_image = 1;
I think this might help you.
Cheers bro
Use LEFT JOIN instead of INNER JOIN.
SELECT * FROM
tish_user LEFT JOIN tish_images
ON tish_user.user_id = tish_images.user_id
WHERE tish_images.prof_image = 1
Explanation
LEFT JOIN selects all rows in the left table, even if there are no entries in the right table (in which case the columns for the right table will be NULL)
Also check RIGHT JOIN, it does the same thing with the right side :)
Try this:
SELECT *
FROM
tish_user U
LEFT JOIN tish_images I
ON U.user_id = I.user_id
AND = I.prof_image = 1
Try this:
Suppose you want to display the userID, firstname and lastname from the tish_user table and the prof_image from the tish_images table.
SELECT tish_user.userd_id, tish_user.firstname, tish_user.lastname, tish_images.prof_image
FROM tish_user tish_user LEFT JOIN tish_image tish_image ON tish_user.user_id=tish_images.user_id WHERE tish_image.prof_image=1
I think this will do.
I have two tables. I want to draw a sample of the first table except where the person in the first table is also in a second table. Am having trouble doing this seemingly simple query.
table users
id|name
table catuser
id|userid|catid
I have tried
SELECT u.*,c.userid FROM `users` u
LEFT JOIN `catuser` c
ON (u.id = c.userid AND c.userid <> '197')
WHERE u.id = '1'
and variations to no avail. Would appreciate any suggestions.
How abt. this:
SELECT u.*,c.userid
FROM `users` u
LEFT JOIN `catuser` c
ON u.id = c.userid
WHERE u.id = '1'
AND c.userid <> '197'
AND c.userid is null
SELECT * FROM users WHERE id NOT IN (SELECT DISTINCT userid FROM catuser)
If you want to query only users that have one or more categories, you can use a WHERE EXISTS query:
SELECT u.* FROM `users` u
WHERE EXISTS (SELECT * FROM catuser WHERE catuser.userid = u.id)
Another possibility is to do a left join, and check whether the join succeeded on checking on null:
SELECT u.*, c.* FROM `users` u
LEFT JOIN catuser c ON u.id = c.userid
WHERE c.id IS NOT NULL
If there is no corresponding row in catuser, all catuser fields will be null. By checking whether c.id is not null, you only include the rows with a category.
Note that the join may return a user multiple time, if he is in multiple categories.