Table users includes columns first,last, and company.
Table columnscontains 5 columns containing different information and a company column.
I was wondering if there is a way to select users record based on the criteria below AND select the row in the columns table that contains the same company name as he comapany column in the users table?
I appreciate any suggestions
Something like:
$st = $this->db->prepare("SELECT * FROM `users`,`columns` WHERE `first`=? AND `last`=? AND `users.company` = `columns.company`");
Assuming that the companies are unique, you can use a LEFT JOIN:
SELECT [column list]
FROM `users` u
LEFT JOIN `columns` c
ON c.`company` = u.`company`
WHERE u.`first` = ?
AND u.`last` = ?
Related
I have a database scheme like this:
All I want is to query data from these tables where the one in the middle is a bridgetable.
I want to write a query to get Details from WrapupCodes table for all the WrapupIds that exists in Bridgetable for the same contactId as in CallDetails table.
e.g:
Similarly this is the table I want to get details from
And here is the main table.
Thanks in advance.
It is very basic SQL:
SELECT *
FROM WrapupCodes w
INNER JOIN Contact_Wrapup c ON c.WrapupID = w.WrapupID
INNER JOIN CallDetail d on d.ContactID = c.ContactID
I have two tables that I'm trying to join, 'holidays' and 'users'.
Users contains all my user info, the the column 'id' being primary and unique.
Holidays contains a column called 'userid', which corresponds to the id in the user table.
I'm struggling to get the join statement to work... what I'm looking for is the result of the select statement to give me the friendlyname (column 'fname' in user table) instead of giving me the value of userid.
Here's what I'm trying...
SELECT * FROM holidays JOIN users on users.id=holidays.userid WHERE holidays.status = 0
But i'm not getting a correct result - SQL executes without error, but my DGV is filled with tons of erroneous results.
Apologies If I have not used the correct terminology or whatever.
I'm new to the concept of joins.
Here is hopefully a better explanation of what I am after...
Thanks in advance.
You need to select the specific values you want from every table in the JOIN:
SELECT u.fname
FROM holidays h
JOIN users u
ON u.id = h.userid
WHERE h.status = 0
by the alias (FROM users u) you can select column from users table by u.fname
First try to right join to the User table. If you just want the fname then select the column name in the SELECT query, as SELECT * takes more time then SELECT column name.
I have 3 sql tables
1) a table with headers of a coupon - id of this equals the id of the second table
2) a tables with details of the coupon - user_id on this tables equals user id of the third table
3) a table with details of user
So far I have this query
"SELECT kpn_processed_deals.kpn_id,
kpn_processed_deals.purchased_date, kpn_processed_deals.claim,
kpn_processed_deals.uid,kpn_deal_headers.kpn_type,
kpn_deal_headers.title,kpn_deal_headers.created_by
FROM kpn_processed_deals INNER JOIN kpn_deal_headers ON
kpn_processed_deals.kpn_id = kpn_deal_headers.kpn_id AND
kpn_deal_headers.created_by = '$var'";
This works just fine but I want to get the value of the users email on the third table using a join but I have been unsuccessful so far. Sorry if my formatting is messy. I'm horrible at these things.
Just add another JOIN.
"SELECT p.kpn_id, p.purchased_date, p.claim, p.uid,h.kpn_type, h.title, h.created_by, u.email
FROM kpn_processed_deals AS p INNER
JOIN kpn_deal_headers AS h ON p.kpn_id = h.kpn_id
JOIN kpn_deal_users AS u ON u.user_id = p.user_id
WHERE h.created_by = '$var'";
Also notice the use of table aliases, so you don't have to repeat the verbose table names throughout the query.
And constraints on single tables should normally be in the WHERE clause; the ON clause is for conditions related to joining the tables (an exception is in outer joins, where constraints on the child table need to be in the ON clause as well).
I am relativly new to the SQL language. I can do a basic select, but for performance increase, I'd love to know if it is possible to merge the two queries I am doing at the moment into one.
Scenario: There are two tables. Table one has a few columns, one of them is a VARCHAR(45) named 'user', and another one is a INT which is called 'gid'. In the second table, there is a primary key column called 'gid' (INT) and a column called 'permissions' which is a TEXT column and it contains values seperated by ';'.
I have a user name, and want the text in the permissions column. The current way I do it is by fetching the gid of the first table, then doing a second query with the gid to get the permissions.
I've heard there are other ways to do this, and I have searched on Google, but I'm not sure what I should do.
EDIT:
Like this:
select t2.permissions
from table1 t1, table2 t2
where t1.user = '<SPECIFIED NAME>'
and t1.gid = t2.gid;
or you could use INNER JOIN syntax:
select t2.permissions
from table1 t1
inner join table2 t2 on t1.gid = t2.gid
where t1.user = '<SPECIFIED VALUE>'
To do this you use a JOIN. A join connects two tables in a select statement.
Like this
select *
from usertable u
join permissiontable p on u.gid = p.gid
This will give you all the columns from both tables with the id column joined. You can treat the joined table just like any table (eg select a sub-set of columns in the select list, add a where clause, etc).
You can read more about joins in any intro sql book or doing a google search.
$sql = "SELECT * FROM `basic` WHERE id = 2 LIMIT 0, 30";
i want to retrieve data from 'users' table in the same query.
fields of users table are
1.country
2.name
3.userid
basic table consist of selling items and there are unique id for each one.
also users table consist of users and there are unique id for each one.
if i say i want data from basic table where id is 4 and user's table where id is 15 in same query....how should i rewrite above mysql statement
May be you need to read this JOIN.Try to read some tutorials that can help you improving your programming knowledge.
$sql=SELECT *
FROM `basic` as b inner join `table2` as c
WHERE b.id =c.id