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'
Related
I am trying to write a query with multiple OR and using DISTINICT for multiple column.But the result is NULL what i am missing in my query. Anyone can help me here please.
SELECT DISTINCT
P.user_p_id,
P.surl,
M.user_id,
M.user_p_id
FROM users P, page M
WHERE (P.user_p_id = '$urlid' OR P.surl = '$urlid' OR M.user_id = '$urlid' OR M.user_p_id = '$urlid')
You should do a proper join with an ON clause:
SELECT DISTINCT
P.user_p_id,
P.surl,
M.user_id,
M.user_p_id
FROM users P INNER JOIN page M
ON M.user_id = P.user_p_id
WHERE '$urlid' IN (P.user_p_id, P.surl, M.user_id, M.user_p_id)
I use ON M.user_id = P.user_p_id although it's not clear if you want these columns to be matched.
You may change the type of the JOIN to LEFT if this is what you need.
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')
I do have a MySQL script that put together two tables using INNER JOIN. Both tables, have a field called ID.
I do need to output with PHP, both ID fields. How do I do this?
This is my script:
$sql = "SELECT a.id,
a.route_id,
a.requester,
a.reservation,
a.reservation_date,
a.reservation_by,
a.telephone,
a.email,
a.firstname,
a.lastname,
a.qty_pax,
a.date_trip,
a.time_trip,
a.trip_type,
a.cancelled,
a.notes,
a.room,
a.driver_id,
b.id,
b.dep_symbol,
b.dep_location_id
FROM
general_reservations a
INNER JOIN
routes b
WHERE
a.cancelled<>'2'
AND a.date_trip BETWEEN '$find_begin' AND '$find_end'
AND b.dep_symbol LIKE '$code'
AND b.id LIKE a.route_id
ORDER BY a.date_trip, b.dep_symbol, a.route_id";
Notice that I do have a.id and b.id from two different tables.
Use an alias name,
$sql = "SELECT a.id AS aid,
a.route_id,
a.requester,
a.reservation,
a.reservation_date,
a.reservation_by,
a.telephone,
a.email,
a.firstname,
a.lastname,
a.qty_pax,
a.date_trip,
a.time_trip,
a.trip_type,
a.cancelled,
a.notes,
a.room,
a.driver_id,
b.id AS bid,
b.dep_symbol,
b.dep_location_id
FROM
general_reservations a
INNER JOIN
routes b
WHERE
a.cancelled<>'2'
AND a.date_trip BETWEEN '$find_begin' AND '$find_end'
AND b.dep_symbol LIKE '$code'
AND b.id LIKE a.route_id
ORDER BY a.date_trip, b.dep_symbol, a.route_id";
Is it possible to use select * for only one table when using a join statement?
Let's say these are the following tables;
B
userID
username
A
userID
entry
....just pretend there are more columns for the sake of this example
What is the correct way to look up the username from table B?
select B.username, * from A
LEFT JOIN B on B.userID = A.userID
where A.entry = "XXXXX"
Or do I have to list out everything I want to select such as:
select B.username, A.userID, A.entry from A
left Join.....
You can use [table name].* to select all fields from one of the tables. For example, to select all fields from table B use:
SELECT B.*, username FROM A
LEFT JOIN B on B.userID = A.userID
WHERE A.entry = "XXXXX"
edit - selected column username from A
SELECT A.* FROM ... where A is the table you want to select from.
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"];