problems with Join query - mysql

Hi I've got data in two tables my first table contains
first name
last name
my second table contain
userid
first name
last name
I'm trying to write a sql query to get the userid of a particular user but I'm getting empty set while executing the query. Could anyone please verify that the query I'm using is right? It seems ok to me
select users.id
FROM TABLE1 AS r
LEFT JOIN TABLE2 AS users
ON (users.firstname = r.firstname
AND users.lastname=r.lastname)

You use twice the same table (TABLE2), but in the description you state that you have two tables.

I am not sure but i think you want this:
select users.id
FROM TABLE1 AS r
INNER JOIN TABLE2 AS users
ON (users.firstname = r.firstname AND users.lastname=r.lastname)

select users.id
FROM TABLE1 AS r
INNER JOIN TABLE2 AS users
ON (lower(ltrim(rtrim(users.firstname))) = lower(ltrim(rtrim(r.firstname))) AND lower(ltrim(rtrim(users.lastname)))=lower(ltrim(rtrim(r.lastname))))

Related

Is there a way of showing names instead of the ID in a SELECT SQL query? [duplicate]

How would I self join a table to show the Name, employee number, manager's name of those who are managed by either Blake or Jones?
I'm trying to line it up in the following manner:
SELECT
FROM
INNER JOIN
ON
WHERE
The problem I am having is I have understood MySQL very well up until now, and I cannot seem to grasp the concept of the table joining itself.... any help would be appreciated. Thanks in advance
MySQL self join that joins a table to itself using join
SELECT *
FROM table1 AS t1
INNER JOIN table1 AS t2
ON t1.col_name=t2.col_name
WHERE t1.col_name='xyz'
select t1.name,t1.employee_number,t1.manager_name from table t1 join
table t2 where t1.manager_name = t2. manager_name and t2.manager_name in
('Blake','Jones');

MySQL Join on LIKE statement

I need to count how many users are in each group in a database. Unfortunately the database design is not great and the users uids are stored against the group in the group table in a LONGTEXT field column name owncloudusers.
Example of owncloudusers data :
{i:0;s:36:"25C967BD-AF78-4671-88DC-FAD935FF1B26";i:1;s:36:"40D6866B-EA06-4F39-B509-8CE551CC1924";i:2;s:36:"7724C600-DE23-45C8-8BFD-326B0138E029";i:3;s:36:"D6FF37EC-11F4-471F-94C9-F3A28416CF1F";i:4;s:36:"F70C6D03-B7BA-44E4-B703-9AF3EED9BC03";}
I thought I could use a query with a LIKE on the join to compare the user's uid and look inside owncloudusers and see if there is a match.
The closest I have got is:
SELECT T1.owncloudname, count(T2.owncloud_name) AS Users
FROM oc_ldap_group_members T1
LEFT JOIN oc_ldap_user_mapping T2 ON T1.owncloudusers LIKE('%:"'||T2.owncloud_name||'";%')
GROUP BY owncloudname;
T1 table holds the groupings and who is tagged to that group
T2 table holds the users data. column owncloud_name is the users uid column
I have tried a few approaches I found on stackoverflow CONCAT on the LIKE join and LIKE('%:"'+T2.owncloud_name+'";%')
But no joy. The current statement I have returns 0 users against all the groups but I know this is not right.
I know it much but an issue on the join not sure where to go with it next.
Any assistance would be much appreciated.
I think you need a simple
SELECT T1.owncloudname, count(*) AS Users
FROM oc_ldap_group_members T1
LEFT JOIN oc_ldap_user_mapping T2 ON T1.owncloudusers LIKE '%T2.owncloud_name%'
GROUP BY owncloudname;
If you need concat try
SELECT T1.owncloudname, count(T2.owncloud_name) AS Users
FROM oc_ldap_group_members T1
LEFT JOIN oc_ldap_user_mapping T2 ON T1.owncloudusers
LIKE concat( '%',T2.owncloud_name,'%' )
GROUP BY owncloudname;
You were close, but mysql doesn't understand || as a text concatenation operator; use CONCAT() with the text parts passed as a list of values to build the LIKE operand:
SELECT T1.owncloudname, count(T2.owncloud_name) AS Users
FROM oc_ldap_group_members T1
LEFT JOIN oc_ldap_user_mapping T2
ON T1.owncloudusers LIKE CONCAT('%;', T2.owncloud_name, ';%')
GROUP BY owncloudname;
if there aint any performance issue,
could you try it with sub-query,
SELECT
T1.owncloudname,
(SELECT COUNT(*)
FROM oc_ldap_user_mapping AS T2
WHERE LOCATE(T2.owncloud_name,T1.owncloudusers)=1) AS Users
FROM
oc_ldap_group_members T1
GROUP BY
owncloudname;

How do I write this SQL Query to join these three tables?

I have three tables that I need to query in order to get my results.
Table 1 has an AppId and a ProjectID
Table2 has an AppID and an AppName.
Table 3 has a ProjectID and a ProjectName
I want to get out of this a list, By AppName, the ProjectNames they are tied to.
So far, a basic query to get what I want works, but I only get the ID's. I need to somehow join these to get the names associated. I need to somehow join this to table2 with the project name information, and table 2 with the appname information.
Select * from Table1 ( this table has only ID's, not names)
Order by AppId
You can join the tables like this:
Select t2.AppName, t3.ProjectName
from table1 t1
inner join table2 t2 on t2.AppID = t1.AppID
inner join table3 t3 on t3.ProjectID = t1.ProjectID

How to use INNER/OUTER JOIN in MYSQL

I have 3 tables which contain different types of data related to each other. the tables populate via an excel spreadsheet. I have:
table1 table2 table3
item_number item_number item_number
desc desc qty_sold
qty_instock vdf_cost upc
cost status
What I'm trying to do is use a join function to show all the data as they relate to each other, except the problem is that when I run
SELECT *
FROM table1 a
INNER JOIN table2 b
ON a.someColumn = b.otherColumn
INNER JOIN table3 c
ON b.anotherColumn = c.nextColumn
It just puts the tables side by side, If I run
SELECT *
FROM table1 a
INNER JOIN table2 b
USING(item_number)
It works but only joins the item number (i have no idea how to use multiple fields such as description which repeats), and for some reason I can only use the two tables when I try to add a third table (most likely being done completely wrong)
SELECT *
FROM table1 a
INNER JOIN table2 b
INNER JOIN table3 c
USING(item_number)
I just get a syntax error.
Thanks for all the help in advance
UPDATE:
I got it working
SELECT *
FROM master_list a
INNER JOIN bby_report ab USING (item_number, description)
INNER JOIN sales_report b USING (item_number)
Is there a way I can exclude the description from one of the tables and keep it from another one? Turns out the descriptions are not an exact match from one table to a another so it keeps returning zero results.
So to clarify keep description from table1 and leave out description from table2 while being able to JOIN the fields based on item_number
SELECT *
FROM master_list a
INNER JOIN bby_report ab USING (item_number, description)
INNER JOIN sales_report b USING (item_number)

MYSQL How to "Join" two Queries

I have 2 tables, one containing the UID of users with a FID (File ID) and one table containg the FID with their URI. I need to get the URI with just the UID.
My first though is to first load all of the FID with a Query and then use the fetched data to make an other query that will load the URI in the second table according to the fetched FIDs.
Since this query is going to be run a lot on this site i was wondering what would be the most efficient way to do so ? Would it be possible to join those 2 queries ?
What you want is a simple join like this:
SELECT uid, uri FROM table1, table2 WHERE table1.fid = table2.fid
SELECT
U.UID,
F.FID,
F.URI
FROM users_Table AS U
JOIN files_Table AS F
ON U.FID = F.FID
WHERE U.UID = "user_identification_number"
It's pretty easy for SQL join.
SELECT `t1`.*, `t2`.`uri`
FROM `users` t1
JOIN `files` t2 ON `t1`.`id` = `t2`.`uid`
If I understood you question correct this could be solved with a simple JOIN
SELECT UID, URI
FROM `table1` as t1, `table2` as t2
WHERE t1.FID = t2.FID;
Read more about how joins work at http://dev.mysql.com/doc/refman/5.0/en/join.html