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.
Related
I need to display details from 2 different tables
Table 1- user table
Table 2-bicycle table
I can show a list of users and bicycle.
But can't show a list that show which bicycle belongs to who.
Bicycle table,
user table
You need to use a LEFT JOIN. The query below will connect your user table to your bicycle table based on the userIDand return all of the bicycle and user fields
SELECT b.*, u.*
FROM registered_users u
LEFT JOIN registered_bicycle b ON (u.userID = b.userID)
If you want specific bicycle fields just prefix them with b. Example:
SELECT b.brand, b.model, b.color...
Click on SQL tab and type this:
SELECT * FROM `registered_bicycle` JOIN `registered_users` ON `registered_users`.`userID` = `registered_bicycle`.`userID`
and run it
You have to make a foreign key from user_table to bicycle_table like this.
user_table has user_id = user_1,name=John Richard
bicycle_table has user_id = user_1,description = Bicycle1
to fetch the data:
select a.user_id, a.name,b.description from
(select user_id, name from user_table) as a
left join
(select user_id,description from bicycle_table) as b
on a.user_id = b.user_id
I am using the Sql query below:
$query = mysql_query("SELECT * FROM `venues`
LEFT JOIN `follows` USING (venue_id)
LEFT JOIN `stats` USING (venue_id, user_id)
WHERE follows.user_id = $userid");
The problem is that it's not showing some fields in the stats table.
So what I would thinking the problem might be (might be wrong), is that I need to select all the fields of that table?
If this is the case, is there a way of telling it to select * the fields for the 3 tables?
For example:
SELECT * FROM `venues`, SELECT * FROM `follows`, SELECT * FROM `stats` LEFT JOIN ....
use alias for each table then
select A., B., C.* from venues as A left join Follows as B.....
You can use alias for tables. Here's your request using those aliases:
$query = mysql_query("SELECT a.*, b.*, c.* FROM `venues` as b
LEFT JOIN `follows` as b USING (venue_id)
LEFT JOIN `stats` as c USING (venue_id, user_id)
WHERE follows.user_id = $userid");
I have the following SQL query:
SELECT * FROM `table1` INNER JOIN `table2` ON table1.messageid=table2.messageid WHERE `venue_active` = 1
The above works fine but it only returns fields where both tables have a messageid field.
My problem is that I need it to return ALL fields from Table1 reguardless if it has a messageid match in table2 or not.
So, in other words I need ALL records to be returned from Table1 and all records from Table2 where there's a messageid that matches both.
How can I do this?
Use a LEFT JOIN rather
SELECT *
FROM `table1` LEFT JOIN
`table2` ON table1.messageid=table2.messageid
WHERE `venue_active` = 1
That said, it will only work if venue_active is also part of table1, and not table2.
Have a look at the different scenarios
SQL Fiddle DEMO
Use a LEFT join rather than INNER
For example:
SELECT * FROM `table1`
LEFT JOIN `table2` ON table1.messageid=table2.messageid
WHERE `venue_active` = 1
Either you need a LEFT JOIN instead, or
a FULL OUTER JOIN workaround for MySQL:
SELECT
a.*,
b.*
FROM
table1 a
LEFT JOIN
table2 b ON a.messageid = b.messageid
WHERE a.venue_active = 1
UNION
SELECT
a.*,
b.*
FROM
table1 a
RIGHT JOIN
table2 b ON a.messageid = b.messageid;
WHERE a.venue_active = 1
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 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'