Very new to using mysql, so please excuse me if this question is really elementary. It would be great if someone could at least suggest what topic I should be researching.
I have 3 tables, user, computer, component. (this is just an example).
user computer component
---- ------- ----
id id id
firstName user_id computer_id
lastName name componentName
type componentValue
A user may have multiple computers, which in turn may have multiple properties.
I am trying to write a query that can get all user ids who have all of their 'Macintosh' computers contain all 'ram' components with '16GB' values.
So far I am struggling with getting the counts right, but have written something like this:
select user.id from user
inner join computer on user.id=computer.user_id and computer.name='Macintosh'
inner join component on computer.id=component.computer_id and component.componentName='ram'
having count(component.propertyName='ram')=count(component.propertyValue='16GB');
Thanks in advance for any constructive feedback.
You could filter for componentValue = '16GB'
select user.id
from user
inner join computer on user.id=computer.user_id and computer.name='Macintosh'
inner join component on computer.id=component.computer_id
and component.componentName='ram'
and component.componentValue = '16GB'
and if you want only the use that have computer with only 16GB ram you could use
select user.id
from user
inner join computer on user.id=computer.user_id and computer.name='Macintosh'
and computer.id not (
select component.computer_id
from component
where component.componentName='ram'
and component.componentValue <> '16GB')
Related
So, I’ve got two tables. Users contains basic user information and user_address contains user address’. Pretty straight forward.
I have a search form that allows for users to be looked up. One search field searches multiple columns using LIKE.
It allows for users to be pulled by their...
company name (listed under users table)
Name (listed under user address table)
Email address (user address table)
Phone 1 (user address table)
Phone 2 (user address table)
The form triggers an Ajax which passes the search value to the search page via $_POST.
$value is then assigned from the $_POST and is filtered and then passed to the mysql query.
On a normal case scenario where both the user exists and addresses exist for the user, the query below works perfectly.
The problem is, that sometimes when a “user” is added to the database, an address isn’t entered right away. During this instance, when trying to search for a user, the user isn’t found.
I need it to find the users whether an address is entered or not. I have tried JOIN, LEFT JOIN, RIGHT JOIN, INNER JOIN, etc. Nothing seems to be working. And for some reason, left join SLOWS down the query and when the query is triggered, the page gets bogged down.
"SELECT u.id,u.company,a.name as address_name
FROM Users as u
JOIN User_Address as a ON a.user_id = u.id
WHERE a.name LIKE '%$value%' OR u.company LIKE '%$value%' OR a.email LIKE '%$value%' OR a.phone_1 LIKE '%$value%' OR a.phone_2 LIKE '%$value%' GROUP BY u.id”
Again the problem with the search is when a user exists in the User table, but no address exists for that user in the User Address table.
Your query is a bit tricky, because if I understand correctly, you want to have both the user and the address, even if the match is just in the user. That's why I would pull the matching into a subquery. This will also massively help with performance.
SELECT Users.id, Users.company, User_Address.name AS address_name
FROM (
SELECT id AS user_id FROM Users WHERE u.company LIKE '%$value%'
UNION
SELECT user_id
FROM User_Address
WHERE a.name LIKE '%$value%'
OR a.email LIKE '%$value%'
OR a.phone_1 LIKE '%$value%'
OR a.phone_2 LIKE '%$value%'
) AS matches
JOIN Users ON Users.user_id = matches.user_id
LEFT JOIN User_Address ON User_Address.user_id = Users.id
GROUP BY u.id
By the way, the LIKE '%$value%' looks like you're using string interpolation to build your query. This can be unsafe. Make sure your values are properly escaped and you understand the dangers and alternatives (like using a parameter binding library).
How can I get all Groups, by the Person ID in this mysql model? I know I need a join colunm or some Hibernate/JPA black magic, but I don't know how to do this.
Here is the model I'm using in study.
Link with image if is not been displayed: http://i.imgur.com/pbCkIVX.png
To reduce space here are the entities:
Github Repository
The following MySQL query will retrieve all groups for a given idPerson
SELECT g.*
FROM `Group` g
JOIN PersonOnGroup pog on g.idGroup = pog.idGroup
WHERE pog.idPerson = myPersonId
I don't know what your hibernate entities look like but something along these lines should work
from Group as group
inner join group.persons as person
where person.idPerson = 1
I'm a bit rusty on SQL and just need a little help thinking this through.
Let's say I have tables for Applications, Ratings and Admins. The idea is that each of the Admins can mark down their Rating for each Application. A Rating has foreign keys for admin_id and application_id.
For the query, I'd like to select all Applications that any particular Admin has not yet rated. Thoughts?
A simple LEFT JOIN perhaps? It basically just returns all rows where there exists no rating from the admin with the particular id.
SELECT a.*
FROM applications a
LEFT JOIN ratings r
ON a.application_id = r.application_id
AND r.admin_id = ?
WHERE r.admin_id IS NULL
I'd write a fiddle, but SQLfiddle is tired again.
SELECT ap.application_id FROM application ap,rating r,admin ad WHERE ad.admin_id=r.admin_id AND ap.application_id NOT IN (SELECT DISTINCT application_id FROM rating) AND ad.admin_id=5;
Does this give the answer?
Haven't tried it myself though, but i think it will..
I have 3 tables called
_partnership,
_partners,
_partnership_arm._partners = stores basic partner information
_partnership_arm = stores partnership arm details
_partnership = stores partners partnership records which includes the partner_id
arm_id which reference _partners.partner_id and _partnership_arm.arm_id.
So as an admin i want to select all details from the _partnership table which join other table reference without a where clause, but am having issue doing it.
here is my code
SELECT
_partnership.*,
_partners.names,
_partnership_arm.arm_name
FROM
`_partnership`
JOIN
`_partners`,`_partnership_arm` ON
_partnership.partner_id = _partners.partner_id
AND
_partnership.arm_id = _partnership_arm.arm_id
I also want a user to be able to select using a where clause
Please how can i achieve this?
Thank you.
SELECT
_partnership.*,
_partners.names,
_partnership_arm.arm_name
FROM
`_partnership`
JOIN
`_partners` ON _partnership.partner_id = _partners.partner_id
JOIN
`_partnership_arm` ON _partnership.arm_id = _partnership_arm.arm_id
I'm a beginner in queries and I'm struggling with one of them. Here are the two tables involved :
The askstobefriends table permit a user to add a friend in the application I m developping. The relational form of it is :
AskToBeFriends(ID (long), #UserAsker (long), #UserAsked (long), Accept (tinyInt))
So with this table we can see who asked to be friend and if it was accepted ...
The query I m trying to realize would permit to list all the user's friends from his ID and also return the friendship statut (accept field ==> waiting for an answer, accepted or refused).
Speretaly, it would be something like that :
SELECT Accept, UserAsker, UserAsked
FROM askstobefriends
WHERE UserAsker = '".$userID."' OR UserAsked = '".$userID."' ";
==> first issue : it can either be the user who asked to be friend with someone or the opposit, that why i've put and OR. After that, I d like that for everyfriend founded there's these informations :
SELECT colUserID, colUserLogin, colUserName, colUserFirstname
FROM userTable
WHERE colUserID == FRIEND
So I guess I need to do a join query, and in my join I have to be sure that I'm using the right foreign key from the asktobefriends tablefor each cases !! (once the key could be UserAsked and another time UserAsker depending on who asked to be friends :S )
Does anyone have a clue please :S ?? Thanks ;-) !!
Your design is wrong. A User asks to be friend of another User, so "Ask_to_be_friend" is the relation, and the cardinality is many to many, so the design will looks like this:
User_User_ID is UserAsker.
User_USer_ID1 is UserAskedtobefriend
and the query could be like (you'll get all the users that user_user_ID Asks to be friend of):
Select U.* from User as U
Join Ask_to_be_friend as A on
U.user_ID = A.User_user_ID
--where a.accept=1 if you add this, this will give
--you all the friends ID of the user_ID table User
If you want to get the names or extra info of the askedtobefriend you'll need a extra Join
Select U.* from User as U
Join Ask_to_be_friend as A on
U.user_ID = A.User_user_ID
Join User as U2 on
A.User_User_ID1=u2.User_ID
--where a.accept=1 ,with this you'll with get only the friends
You could join the tables using criteria that ensure only friends of :userID are returned. For example:
SELECT u.*, a.Accept
FROM askstobefriends a JOIN userTable u ON (:userID, u.colUserID) IN (
(a.UserAsker, a.UserAsked),
(a.UserAsked, a.UserAsker)
)