MYSQL | Request from two tables 2 - php

I have 2 Mysql tables:
A (Users table):
id username room
1 User1 1
2 User2 1
3 User3 1
B (Blocked users):
id username
1 User3
My request is here (I would like to get User1 and User2 as User3 is in block):
SELECT A.id, A.username FROM `table1` A, `table2` B
WHERE A.roomid = 1 AND A.username != B.username
But this is wrong request.
Thanks!

You need to either join the two tables A&B like
SELECT A.id, A.username FROM `table1` A
JOIN `table2` as B ON `table1`.id = `table2`.id
WHERE A.room = 1 AND A.username != B.username
OR your column name is room not roomid like
SELECT A.id, A.username FROM `table1` A, `table2` B
WHERE A.room = 1 AND A.username != B.username

SELECT A.id, A.username,B.id,B.username FROM `table1` A, `table2` B
WHERE A.roomid = 1 AND A.username != B.username

In table A what is mean by ID? is it the id of the username?
In table B what is mean by ID?
you have to use sql join to get the expecting out put. but to do that you need a common key in both table. According to your table example I think that ID is not the common key and you just use it. but If I wrong, you can join the two tables using inner join while getting the ID as the common key.

Related

How get selected rows where user id is not in table record mysql

How get selected rows where user id is not in table record
the table like this
table_a
id subid userid
1 2 123
2 4 123
table_b
id title
1 like
2 liked
3 bookmark
4 bookmarked
i use join table to get the result
if WHERE userid=123 then get result
id title
2 liked
4 bookmarked
the query
select b.id,b.title from
table_a a
LEFT JOIN table_b b
ON b.id = a.subid
WHERE a.userid = '123'
but, how if userid = '345' and not in table_a get the result too?.. the result is must like this (selected rows)
id title
1 like
3 bookmark
Thankyou
You can use the following query:
SELECT id, title FROM t2 LEFT JOIN t1 ON t1.subid = t2.id
WHERE (t1.userid = 345 AND NOT userid IS NULL) OR (
NOT EXISTS(SELECT userid FROM t1 WHERE userid = 345) AND userid IS NULL
);
The working examples you can find here: http://sqlfiddle.com/#!9/c6a446/25
Try using this query
select a.no, b.title from a join b on (b.no=a.no) where a.userid='345'
You can use left join and get the data for the table where their is no match. I have solved this recently.
Chk this link:
Least Popular Event - Get list of events where bookings IS NULL OR LEAST
In mysql
SELECT b.no,b.title FROM a LEFT JOIN b ON a.subid = b.no and where a.userid IS NULL;
SELECT b.no,b.title FROM a LEFT JOIN b ON a.subid = b.no and where a.userid <> '345';
in laravel :
$least = bmodel::leftJoin('a', function($join)
{
$join->on('a.subid', '=', 'b.no');
})
->select('b.no','b.title')
->whereNull('a.userid')
->first(); or get()
You can use this query to get one or more results. It basically compares userid column with no column and gets list of no and title where there is no match in table a.
This basically would select with given userId or if that is not present it then selects the result which
SELECT * FROM t2 LEFT JOIN t1 ON t1.subid = t2.id WHERE t1.userid = 123||
(select userid from t1 where userid =123LIMIT 1) is null
This is the second case where you will get all the rows where it is not 123 or any number
SELECT * FROM t2 LEFT JOIN t1 ON t1.subid = t2.id WHERE t1.userid = 345 ||
(select userid from t1 where userid =345 LIMIT 1) is null

Multiple WHERE conditions in 2 tables in MySQL

I have 2 tables as follows:
table1
ID Name Test
A011 John 1
A012 Lynda 1
A013 Micheal 1
A014 Jack 0
A021 Joe 1
A015 Paul 0
table2
ID Done
A011 1
A012 1
I want to select all rows from table1 that have an ID where the 3 first letters are equal to A01, and the test field is 1, and also the ID is not present in table2.
I tried this query:
SELECT a.* FROM table1 a LEFT JOIN table2 b ON a.ID = b.ID
WHERE a.test = 1 AND b.ID IS NULL
The result from that is 2 rows with ID A013 and A021. I tried to use LEFT(ID,3) to get the ID with A01, however, I couldn't achieve what I want.
How can I filter only the records where the ID starts with A01?
Try this, it will give you the desired result
SELECT t1.* FROM table1 t1 LEFT JOIN table2 t2 ON t1.userid = t2.userid WHERE LEFT(t1.userid , 3) LIKE '%A01%' AND t1.userid NOT IN (SELECT userid from table2)
SELECT * FROM table1
WHERE test = 1
AND ID LIKE "AO1%"
AND ID NOT IN (SELECT ID from table2)

How to query for One - Many Relation

I have two database tables with "one to many" relationship
let say, table A and Table B
One field from Table A is a foreign key in table B
I wanna fetch just one record from table A as well as table B (given table A's primary key)
table A
id name
--------------------
1 ABC
2 XYZ
Table B
id A_id email
------------------------------------------------
1 1 temp#temp1.com
2 1 temp#temp2.com
3 1 temp#temp3.com
4 2 temp#temp4.com
4 2 temp#temp5.com
Answer should be like this (Single Record From Table B)
For a.id = 1
A.id, A.name,B.email
-------------------------
1, ABC, temp#temp1.com
For a.id = 2
A.id, A.name,B.email
-------------------------
1, XYZ, temp#temp4.com
I used this query, but it returns all the records from table B(as table B has multiple records for each record in Table A)
SELECT a.id,a.name, b.email FROM A a, B b WHERE a.id = 1 AND b.A_id = a.id
I have created a SQL Fiddle for the demo here: http://www.sqlfiddle.com/#!2/15ae7/5
The query that you can use for getting the desired output is:
SELECT
tableA.id,
tableA.name,
tableB.email
FROM tableA
LEFT OUTER JOIN tableB ON tableB.A_id = tableA.id
GROUP BY tableB.A_id;
For more information on JOINS and GROUP BY you can refer to the following pages:
http://dev.mysql.com/doc/refman/5.0/en/join.html
https://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html
Try a JOIN
SELECT
a.id,
a.name,
b.email
FROM A a
LEFT JOIN B b ON b.A_id = a.id
WHERE a.id = 1;
GROUP BY b.A_id
More info on JOINS
You can use LIMIT if you want only one record.
SELECT a.id,a.name, b.email FROM A a, B b WHERE a.id = 1 AND b.A_id = a.id LIMIT 0,1
Hope this helps you.

Select same field on 2 table with same alias

Suppose this:
users
id | name | address
partners
id | name | company | address
Even though the tables are distinct sometimes happens having to associate the users or partners id to the same function ..
For example, the access table
acl
uri | uid | group | operations
Here "uid" can be both user and partner.
How can I read the data of 2 tables with the same alias?
something like:
$selectQuery = <<<QUERY
SELECT A. *,
U.name P.name AS username,
G.name AS groupname
FROM [acl] A
LEFT JOIN [users] U ON A.uid = U.id
LEFT JOIN [partner] P ON A.uid = P.id
LEFT JOIN [groups] G ON A.gid = G.id
WHERE A.id =: id
LIMIT 0.1
QUERY;
You probably want to use COALESCE or IFNULL
IFNULL(U.name, P.name) AS username,
COALESCE(U.name, P.name) AS username,
Both of those will do the exact same thing in this situation. If U.name is not NULL then username will be U.name, otherwise it'll be P.name.
USE UNION ALL with the condition......it should help hopefully

Getting a list of friend's names from MySQL

I have a table named friends;
friends
id uid fid
1 1 2 (1 is a friend of 2 and vice versa)
2 1 3 (1 is a friend of 3 and vice versa)
3 4 1 (1 is a friend of 4 and vice versa)
4 5 1 (1 is a friend of 5 and vice versa)
And a corresponding table for users;
users
uid name
1 mike
2 john
3 karl
4 mary
5 tony
This doesn't seem to do the trick:
SELECT name FROM users LEFT JOIN friends ON friends.uid=users.uid WHERE friends.uid='1' OR friends.fid='1'
What should my query be to get all the names of mike's friends?
This should do it just fine with a single, easy to index, query;
SELECT name FROM users u
JOIN friends f
ON u.uid = f.uid OR u.uid = f.fid
WHERE (f.uid=1 OR f.fid=1)
AND u.uid<>1;
Demo here.
Untested:
SELECT name from friends LEFT JOIN users on users.uid=friends.fid where friends.uid=1 UNION
SELECT name from friends LEFT JOIN users on users.uid=friends.uid where friends.fid=1
This may look a little strange if anyone is ever friends with themselves.
try one of these:
SELECT a.uid as UserID,
a.`Name` as UserName,
c.`Name as FriendsName
FROM users a LEFT JOIN friends b on a.uid = b.uid
LEFT JOIN users c on b.fid = c.uid
OR
SELECT a.uid as UserID,
a.`Name` as UserName,
GROUP_CONCAT(c.`Name`) as FriendsList
FROM users a LEFT JOIN friends b on a.uid = b.uid
LEFT JOIN users c on b.fid = c.uid
GROUP BY a.uid
As in your prequel question, you need to cover both foreign keys to the user table to get all his friends:
SELECT users.*
FROM (
SELECT uid FROM friends WHERE fid = 1
UNION ALL
SELECT fid FROM friends WHERE uid = 1
) f
JOIN users USING (uid)
Switch friends and users in your query and I think you'll get what you want.
In other words:
SELECT name FROM friends LEFT JOIN users ON friends.uid=users.uid WHERE friends.uid='1' OR friends.fid='1'
I think this is right SELECT name FROM users LEFT JOIN friends ON friends.uid=users.uid WHERE friends.uid=1 OR friends.fid=1

Categories