Suppose I have two table
Table Name group
column 1: giver
column 2: acceptor
Table name userinfo
column 1: name
column 2: status
i want to select giver,acceptor and userinfo.status that from group table where giver or acceptor whose name is zakir that giver or acceptor exist in uerinfo table as name.
Need Help to write sql statement for taht query..
Thanks in advance... :)
What you are referring to is also known as the INNER JOIN clause in an SQL Statement.
Depending on the relationship you can create an INNER JOIN to potentially connect the two variables that are identical.
SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name=table2.column_name;
Extracted from W3Schools.com
Try the below query,
SELECT
gp.giver,gp.acceptor,ui.status
FROM group as gp JOIN userinfo as ui
on ui.name = 'zakir'
AND (gp.giver = 'zakir' or gp.acceptor = 'zakir')
Or try this without join,
SELECT
gp.giver,gp.acceptor,ui.status
FROM group as gp, userinfo as ui
WHERE ui.name = 'zakir'
AND (gp.giver = 'zakir' or gp.acceptor = 'zakir')
Try this:
SELECT `group`.`giver`, `group`.`acceptor`, `userinfo`.`status`
FROM `group`, `userinfo`
WHERE (`group`.`giver` = 'zakir' OR `group`.`acceptor` = 'zakir')
AND `userinfo`.`name` = 'zakir'
select g.giver, g.acceptor u.status
from group g, userinfo u
where u.name = 'zakir'
and (g.giver = u.name or g.acceptor = u.name)
It should do a part of the job.
This assumes you want the status for both the giver and the acceptor
May need a little tweaking for mysql syntax
Select giver, g.status, acceptor, a.status
FROM GROUP
join userinfo as g on group.giver = g.name
join userinfo as a on group.acceptor = a.name
where (giver = 'zakir' or acceptor = 'zakir')
Related
I want to make my keyword search in two mysql tables. my tables don't have any identical column names. But I tried few queries, they didn't work for me.
Keyword IS 07731A0328
I tried this:
$sql = "select a.*, b.* from table1 a inner join table2 b on a.col1=b.htno WHERE a.col1 like '$name'";
$sql = "select a.*, b.* from table1 a join table2 b on a.col1=b.htno WHERE a.col1 like $name";
Can someone help me with this? Thank you!
TABLE 1
TABLE2
Join is your friend:
http://www.w3schools.com/sql/sql_join.asp
Combine rows from two or more tables, based on a common field between them.
SELECT * FROM TABLE1 INNER JOIN TABLE2 ON TABLE1.col1=TABLE2.htnon
WHERE TABLE1.col1 = "07731A0328"
The query will be
SELECT * FROM Table1,Table2
WHERE Table1.col1=Table2.htnon AND Table1.col1 = "07731A0328"
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 can do the following in 2 queries but want to make it simpler. Can this be combined in one query? If so how more efficient is it than doing two queries vs one?
query1: SELECT page_id, coupon_id from table_1 WHERE key = :key
query2: SELECT folder from table_2 WHERE page_id = table_1.page_id
For my final result I need to have a coupon_id from table_1, and a folder from table_2.
In query2 I need to use the page_id result from query1 to get the folder
Is there a simpler way to do this?
Use JOIN (LEFT, RIGHT or INNER is up to your needs):
SELECT
t1.page_id,
t1.coupon_id,
t2.folder
FROM
table_1 AS t1
LEFT JOIN table_2 AS t2 ON
t2.page_id = t1.page_id
WHERE
t1.key = :key
You will want to JOIN the tables on the page_id:
SELECT t1.page_id,
t1.coupon_id,
t2.folder
from table_1 t1
inner join table_2 t2
on t1.page_id = t2.page_id
WHERE key = :key
If you need help learning join syntax, here is a great visual explanation of joins.
I used an INNER JOIN which will return all rows that match between the two tables. If you want to return all rows from table_1
even if it doesn't have a matching row in table_2, then you would use a LEFT JOIN
SELECT
table_1.coupon_id AS coupon_id,
table_2.folder AS folder
FROM
table_1
INNER JOIN table_2 ON table_2.page_id = table_1.page_id
WHERE
table_1.key = :key
SELECT t1.page_id, t1.coupon_id, t2.folder
FROM table_1 t1 LEFT JOIN table_2 t2 ON (t1.page_id = t2.page_id)
WHERE t1.key = :key
This will be faster than two queries, how much depends on your data.
Try this please:
SELECT a.page_id, a.coupon_id, b.folder_id
from table_1 a
join table_2 b
ON a.page_id = b.page_id
WHERE a.key = :key
group by a.page_id
;
I have the code here to join two tables. However I don't want to get the password element from the Accounts database. How could I do this?
"SELECT f.*, a.*
FROM Following as f
JOIN Accounts as a on f.followingUserID = a.id
WHERE `followingUserID` = '$acID'
There is no SQL convention for "all columns EXCEPT FOR ..." -- it's either all, or you define the list by hand:
SELECT f.*,
a.col1, a.col2,
a.`col name using spaces not good`
FROM FOLLOWING as f
JOIN ACCOUNTS as a on f.followingUserID = a.id
WHERE f.followingUserID = '$acID'
Name the columns instead of retrieving them all.
Instead of a.*, :
a.ColumnName1, a.ColumnName2, etc....
If you don't want to select a password element you will need to change the a.* to select each column individually i.e.
SELECT f.*, a.account_id, a.name
FROM following as f
JOIN accounts as a on f.followingUserId = a.id
WHERE followingUserID = '$acID'
hello please help me out regarding this query ,I am fetching data from different table The problem i am facing is that in the table there are similar colum name like employee have and user has also name . The query work perfectly but i am wordering about how i can display this data as
$data["employee.name"]
$data["user.name"]
here is the query:
SELECT task.employee_id , task.user_id , task.service_id, user.name,
user.pic_path , employee.name ,employee.pic_path
FROM task
INNER JOIN employee ON employee.pno = task.employee_id
INNER JOIN user ON user.pno = task.user_id
INNER JOIN service ON service.service_id = task.service_id ";
SELECT user.name AS username, employee.name AS employeename
You get the point.
There are two steps:
You need to define a column alias for at least one of the two columns in the SQL statement:
SELECT t.employee_id,
t.user_id,
t.service_id,
u.name AS user_name,
u.pic_path,
e.name AS employee_name,
e.pic_path
FROM TASK t
JOIN EMPLOYEE e ON e.pno = t.employee_id
JOIN USER u ON ur.pno = t.user_id
JOIN SERVICE s ON s.service_id = t.service_id
Then you need to update the PHP logic to use the column aliases:
$empname = $data["employee_name"];
$username = $data["user_name"];