Query to fetch data from two different tables - php

I have two tables. Table A and B.
I have a log-in form where I will get the username,password, assign, assign2 of the user from table A and fetch data depending on the user's assign1 and assign2 from table B.
How my query would look like?
Looking for answers. Thankyou so much.
Table A -THIS IS THE TABLE FOR LOG-IN FORM
==========================================
username | password | assign1 | assign2 |
------------------------------------------
SANDRA | SANTOS | 1 | 1 | //Values
--------------------------------------------
Table B -
=======================================
name | assign1 | assign 2 |
------------------------------
DADA | 1 | 1 | //this will be displayed
------------------------------
gorg | 2 | 2 |
//this must not be displayed since the user assign1 and assign2 who logged in did not match to this

You are using $sql variable in
$result = $con->query($sql);
It should be $queryagain.

Your question is not clear, but if you want to fetch data from Table B which depends on assign1 and assign2 you can do it like this:
$queryagain = mysqli_query("SELECT * FROM tableB INNER JOIN tableA ON tableB.name=tableA.username WHERE tableA.assign1 = tableB.assign1 AND tableA.assign2 = tableB.assign2");
When you use JOIN it's best practice to JOIN by primary key (id). If tableB.name=tableA.username are not columns with same value you can join by other columns, like assign1, or assign2:
assign1:
$queryagain = mysqli_query("SELECT * FROM tableB INNER JOIN tableA ON tableB.assign1=tableA.assign1 WHERE tableA.assign2 = tableB.assign2");
assign2:
$queryagain = mysqli_query("SELECT * FROM tableB INNER JOIN tableA ON tableB.assign2=tableA.assign2 WHERE tableA.assign1 = tableB.assign1");
Notice: Where clause is not necessay, you can edit WHERE clause depends of the result you want.

I think your are very very new to database programing. First of all I recomend you to read https://www.w3schools.com/sql/sql_join.asp this page and work on examples using try it yourself part.
Join Types:
INNER JOIN: Returns records that have matching values in both tables. That means if assign field is filled in Table A and the value is included in table B then this type of join should match the rows.
LEFT JOIN: Return all records from the left table, and the matched records from the right table. That means all rows in Table A will match either the value is included in table B or not.
RIGHT JOIN: Return all records from the right table, and the matched records from the left table. That means all rows in Table B will match either the value is included in table A or not.
FULL JOIN: Return all records when there is a match in either left or right table. All rows in table A and table B will be included in te result set either they match or not.
Your SQL query may look like:
$sql = "SELECT * FROM table_a a INNER JOIN table_b b on a.assign1 = b.assign1 INNER JOIN table_b b2 on a.assign2 = b2.assign2";
execution part of the sql can be different depending your database engine or other libraries.
I think this thread also helps you: Join two mysql tables with php

Related

I am working On SQL query but not working full outer join

I want to work on SQL. inner join is not working missing. I am using two tables get data on "city_id " field these table code so please help me
inventory
==========================================
inventory_id | city_id | title |is_enabled
==========================================
1 | 1 | abc | 1
2 | 1 | bcd | 1
cities
====================================
city_id | city | title
===================================
1 | delhi | abc
2 | nodia | bcd
SELECT * FROM inventory i
INNER JOIN cities c
ON i.city_id = c.city_id
WHERE i.is_enabled = 1
ORDER BY i.inventory_id DESC LIMIT 10;
Some databases (such as MS Access and MySQL) do not support FULL OUTER JOIN.
However, FULL OUTER JOIN is rarely needed -- and almost never when you are using keys defined between tables. Well structured databases have keys with appropriate values.
I suspect an INNER JOIN does what you want:
SELECT . . . -- list out the columns you want
FROM inventory i INNER JOIN
cities c
ON i.city_id = c.city_id
WHERE i.is_enabled = 1
ORDER BY i.inventory_id DESC
LIMIT 10;
This query assumes that the inventory rows have a valid city_id. If some are NULL (an allowed, non-matching value), you can use LEFT JOIN instead.
Some other notes:
List out the columns you want explicitly. In particular, duplicate column names can be problematic.
Use table aliases, so the query is easier to write and to read.
Qualify all column names. I'm guessing that is_enabled comes from inventory. I should not have to guess.
Do not put single quotes around numeric constants. I am guessing that is_enabled is a number, not a string.

mysql join two different tables

I have two tables in my MySql database:
user
- sid
- userid
- username
log
- sid
- userid
- login_time
As you can guess, there's a lot more records in log tables than in user table.
I am using php to present these records on my website in a table format as shown below.
no | userid | username | number of login |
1 | inzo | harvey | 233 |
2 | chae | schmidts | 433 |
3 | hibro | swainy | 12 |
To get the number of login for each user, I can send another queries in a for statement. But it's consuming resources and making the server slow in the end.
Can I have this result in one single join query?
Yes you can, you have to use count the logins for each user with a group by
select t1.userid, t1.username, count(t2.sid)
from user t1
left join
log t2
on t1.userid = t2.userid
group by t1.userid, t1.username
The left join ensures you that users without logins will still be returned, wit 0 as count.
Edit
About the question in the comment: if you want to only count the logins with a specific flag value, you can just add where flag = x before the group by; if you want to have a separate count for each value of the flag, you have to add that flag to both group by and select.
I guess best and by that I mean least resource consuming way would be to add "number_of_login" to user table and just increase it every time he/she is logged in, because any other solution will require looping
SELECT TABLE_A.row_id, TABLE_A.category, TABLE_A.val_1, TABLE_B.val_2
FROM TABLE_B
LEFT OUTER JOIN TABLE_A ON TABLE_B.row_id = TABLE_A.row_id
ORDER BY row_id;
If you want all the results, you need an outer join, not an inner one.
select a.sid,a.userid,a.username,COUNT(b.sid) from user a
Left join log b ON b.sid =a.sid group by a.sid

How to find rows in a MySQL database that don't correlate correctly with other rows

I have a table in my MySQL database that, in part, looks like this:
+----+-------+-------+
| id | owner | onwed |
+----+-------+-------+
| 1 | A | B |
| 2 | B | A |
| 3 | C | D |
| 4 | D | C |
| 5 | E | X |
+----+-------+-------+
They important feature here is that all the entries are paired, so that each "owner" is also "owned" by the record they correlate to. A "owns" B, and also B "owns" A. D "owns" C, and also C "owns" D.
However, in row 5,we have a problem. E "owns" X, but there is no entry for X "owning" E.
I need to be able to go through this table, which has thousands of records, and find all instances like row 5, where we have an orphaned record. Where there is no corralating with the "owner" and "owned" fields having a matched opposite. Also, there is no assurance that paired rows will follow each other as they do in my example.
This problem goes way beyond my MySQL abilities. I know how to do a search when I know what the value I'm looking for is. But I don't know how to go through each row one by one, take values out, and then use those values to do another search. I hope someone can help me out and I apologize that I'm so clueless on this matter that I don't have any code to suggest.
I'm not so worried about efficiency, in that this is a check I would only run every now and again when there is reason to suspect a problem. Also, if it helps, I manage my MySQL database from a PHP script, so if there is PHP code that can be leveraged to make the task more manageable, that could also be utilized.
What you need is a join. If you join the table on itself with the owned on owner.
SELECT T1.Id, T1.Owner, T1.Owned, T2.Id, T2.Owner, T2.Owned
FROM tablename T1
LEFT JOIN tablename T2
ON T1.Owned = T2.Owner
WHERE T2.Owned != T1.Owner
OR T2.Id IS NULL
Try to run the query without the WHERE clause to see what this join does. You get both owners and owned on a single row and then you can compare if they match.
You could try a query similar to (not sure about the backquotes of MySQL)
SELECT * FROM `table`
WHERE (`owner`,`owned`)
NOT IN (SELECT `owned`,`owner` FROM `table`);
There are probably cleaner solutions without selfjoins
There are better ways, but this will work:
SELECT
*
FROM
table
WHERE
id NOT IN (
SELECT
id
FROM
table s1
WHERE
s1.owner = (
SELECT
onwed
FROM
table s2
WHERE
s2.onwed = s1.owner
)
);
Its easy:
select t1.id from tableName as t1 left join tableName as t2
on t1.owner = t2.owned and t1.owned = t2.owner
where t2.id is null
select
*
from
mytable
where id not in (
select
m1.id
from
mytable m1,
mytable m2
where
m1.owner=m2.owned
)
You can try this- demo below :
http://sqlfiddle.com/#!2/0db47/7
SELECT a.id, a.owner, a.onwed
FROM TableName a
LEFT JOIN TableName b
ON (a.Owner = b.Owner AND a.Onwed = b.OnWed) OR
(a.Owner = b.OnWed AND a.Onwed = b.Owner)
GROUP BY a.id, a.owner, a.onwed
HAVING COUNT(*) = 1
SQLFiddle Demo

PHP MySQL Select ID from one table and information from another table

I have two tables, one table is called queuelist and the other is call info. In the queuelist table it just lists different IDs. I am trying to get the 'clientID' from that table and match it with the 'ID' in the other table that contains all of the info and display it back on the page. Here is how the tables look:
Table - queuelist
ID | clientID
-------------
1 | 589
2 | 254
3 | 486
Table - info
ID | Name | Phone
--------------------
256 | Bob | 5551231234
486 | Jack | 5551231234
589 | Jill | 5551231234
This is what they call joining tables, you should use a query like this:
SELECT i.ID, i.Name, i.Phone FROM `queuelist` AS q
LEFT JOIN `info` AS i ON (
q.clientID = i.ID
);
I'm using aliases for shorter notation in the above query (queuelist becomes q and info becomes i) and then set the join condition (the bit between the ON()) to be the clientID from the queuelist table should match the ID in the info table.
Also see http://dev.mysql.com/doc/refman/5.0/en/join.html for more details.
You need to use an inner join
select * from queuelist as ql inner join info as i on ql.clientID = i.ID
Though you might want to replace * with specific field names e.g
select ql.clientID, i.fieldname FROM....
Well, I see no difficulty in this using a JOIN.
SELECT * FROM queuelist JOIN info ON clientID = info.ID WHERE queuelist.ID = 2
"Where" would be another option.
SELECT Name, Phone FROM queuelist,info WHERE clientID = ID
Assuming you want only name and phone

Nested or Joins query for MySQL using PHP

I have many a times tried using nested query for MySQL in PHP, but it does not work. Is it not possible to do nested/Joins queries?
Just a Scenario:
I have two tables one table with user id and the other with data. User logins and with sessions I have to cross check two different tables with user id (user and data). Is it not possible to nest/join these two tables to write a single query statement.
In short is nesting or joining two or more tables permitted in PHP coding?
YES, it is possible to join two or more tables in MySQL (and therefore, also when using PHP).
You need to post your table schema, if you want us to show a relevant join query. You could, however, try something like:
SELECT * FROM user AS t1
CROSS JOIN data AS t2
ON t1.userid=t2.userid
WHERE t1.userid='154'
(This query presumes that there always will be one row with the userid in both tables. You should use LEFT JOIN instead of CROSS JOIN to return a row even if there is no row in data for the userid. 154 is just an example userid.)
Have a look at http://dev.mysql.com/doc/refman/5.5/en/join.html for information on the JOIN syntax.
users
| user_id | username | password | enabled |
|---------|----------|----------|---------|
| 1 | john | sgsd2gg | 1 |
| 2 | jane | sdshdhd | 0 |
users_data
|udata_id| user_id | some_column |
|--------|---------|-------------------|
| 1 | 1 | Some title |
| 2 | 2 | another title |
Since you haven't posted your table schema, I can't give you an exact solution. But supposing you have a users table and a users_data table, where users_data are owned by a user. You can do a join on the table to retrieve all the data.
SELECT * -- Don't select all fields unless you need it
FROM users U LEFT JOIN users_data UD ON U.user_id = UD.user_id
WHERE U.user_id = 1
This would pull all the records for user with an ID of 1. This is a very simplistic join, but it should give you an idea.
Here's an example that visually describes the different options you can use : SQL Join Differences

Categories