I have table named 'company' and 'user'. The user table contains the id of company.
Like for example:
A. User table
user_id | name | company_id |status
1 | john | 1 | active
B. Company table
company_id | name | status
1 | ABC | active
How to get the name of the company by their id in single sql query.
such;
$query = "SELECT name as Username, company_id as Company_Name From `user` where status='active'";
That will give the result of:
Username | Company_Name
john | ABC
Any help or ideas on how to do this...
Thanks in advance.
SELECT u.name AS Username, c.name AS Company_Name
FROM User AS u
INNER JOIN Company AS c
ON u.company_id = c.company_id
WHERE u.status = 'active'
AND c.status = 'active'
Feel free to remove either or both of the expressions in the WHERE clause if they don't make sense.
Related
Hello I am trying to get number of unread replies from administrator. Those are support tickets. There are three tables like below.
table: supportticket
id | date | username | email | status | subject | message | adminreply |
admindate | read
table: ticketreplies
id | tid | userid | name | email | date_added | message | admins
table: tickets
id | userid | trackid | name | email | date_added | title | message |
status | urgency | lastreply
table: ticket_read
id | idt | lu
I tried this:
$query = "SELECT COUNT(*) FROM first table WHERE to_user_id
=".$_SESSION['user_id']." AND `read` = 0";
But without success. Hope for more ideas. I am new at this.
*when ticket is created by user this table gets update:
tickets
id | userid | trackid | name | email | date_added
| title | message | status | urgency | lastreply
456 2 123-456 user user#user.com 3/23/2017
15:52 test test open medium
3/24/2017 15:52
* when admin replies
ticketreplies:
id | tid | userid | name | email | date_added
| message | admins 0 456 0
3/23/2017 15:52 replied to test 1
ticket_read
id | idt | lu
0 456 1
p.s. when admin replies tickets table gets status Answered and lastreply get new date time
check image: prntscr.com/eo0pj2
I am sure it is not to_user_id but id in first_table so change like this
$query = "SELECT COUNT(*) FROM `first_table` WHERE `id`
='".$_SESSION['user_id']."' AND `read` = 0";
Assuming you have not provided exact table name that why it is first table not as first_name
Try like this
$query = "SELECT COUNT(*) FROM first table
WHERE first_table.id =".$_SESSION['user_id']." AND `read` = 0";
Since not much information is given in the question (no logical connection between tables is provided in the question), then in case id is just a reference number of the ticket you could try this.
$query = "SELECT COUNT(*) FROM supportticket
INNER JOIN tickets
ON supportticket.id=tickets.id
WHERE tickets.userid =".$_SESSION['user_id']." AND supportticket.read = 0";
OR this
$query = "SELECT COUNT(*) FROM supportticket
INNER JOIN ticketreplies
ON supportticket.id=ticketreplies.id
WHERE ticketreplies.userid =".$_SESSION['user_id']." AND supportticket.read = 0";
As I understand your question and table structure, You should use INNER JOIN to this. And make some changes to your tables.
Example:
SELECT COUNT(*) as admin_replied FROM first_table
INNER JOIN second_table ON first_table.trackid = second_table.trackid
WHERE user_id=".$_SESSION['user_id']." AND `read` = 0";
The first_table should contain trackid as your second_table does. This ticket id will be your key to get all the unread reply of admin in a particular user.
But this will be change according on how you store the message and replies to your tables.
I have two tables, for simplicity, table A and table B (Note, I don't have the authority to change the structure of these tables, so I'm asking this question to get around a bad database design).
Table A has two columns:
"id" - is the unique identifier.
"customer_id" - is the customer's ID.
So table A holds a list of customer IDs.
Table B holds properties about customers. But it does it in a weird way (again, I didn't set this up, and I can't change it). Table B has [NUMBER] columns:
"id" - is the unique identifier.
"customer_id" - is the customer's ID.
"key" - is the name of the key/value pair
"value" - is the value of the key/value pair
So table B holds key/value pairs that are linked to customers by their ID.
I could join these two tables to get something like this:
+----+-------------+------------+-------+
| id | customer_id | key | value |
+----+-------------+------------+-------+
| 0 | 5 | first_name | Bob |
| 1 | 5 | last_name | Jones |
| 2 | 6 | first_name | Sally |
| 3 | 6 | last_name | Sue |
+----+-------------+------------+-------+
But as you can see, that can be difficult to manage because information about one customer is on two different rows. What would be ideal is something like this:
+----+-------------+------------+-----------+
| id | customer_id | first_name | last_name |
+----+-------------+------------+-----------+
| 0 | 5 | Bob | Jones |
| 1 | 6 | Sally | Sue |
+----+-------------+------------+-----------+
With all of the customer's data on one row.
Is there a way to do this in a SQL query so that I don't have to mess with the results in PHP? Or will I have to pick through the data in PHP?
Given that you cannot alter the table structure, one approach is to use subselects on the customer_id and key:
SELECT
tableA.id,
tableA.customer_id,
(
SELECT
tableB.`value`
FROM tableB
WHERE tableB.customer_id = tableA.customer_id
AND tableB.`key` = 'first_name'
) AS first_name,
(
SELECT
tableB.`value`
FROM tableB
WHERE tableB.customer_id = tableA.customer_id
AND tableB.`key` = 'last_name'
) AS last_name
FROM tableA
Note: Performance-wise this query might suck. But, if you are out of options, maybe the slow query will drive the people who make decisions to allow the structure to be changed.
Use Group_concat or Group by
Select *,Group_concat(value) as full_name
From b left join a on b.customer_id=a.customer_id
Group by customer_id
One method is conditional aggregation:
select (#rn := #rn + 1) as id, customer_id,
max(case when `key` = 'first_name' then value end) as first_name,
max(case when `key` = 'last_name' then value end) as last_name
from b cross join
(select #rn := 0) params
group by customer_id;
I'm not sure what table a would be used for, perhaps for filtering the customer ids.
This will do what you want:
SELECT A.customer_id, B.value, B_1.value
FROM (A INNER JOIN B ON A.customer_id = B.customer_id)
INNER JOIN B AS B_1 ON B.customer_id = B_1.customer_id
WHERE (((B.key)="first_name") AND ((B_1.key)="last_name"));
Hello I would like to ask how I can select different values from different tables based on the id's from my main table properties table:
Here is how my DB is organized:
propery table:
----------------------------------------------------
id | title | property_type | city | sector | owner |
----------------------------------------------------
1 | title | 1 | 2 | 6 | 12 |
----------------------------------------------------
property_type table:
--------------
id | english |
--------------
1 | name |
--------------
2 | name 2 |
--------------
owner table:
-----------------------------------
id | ownername | phone |
-----------------------------------
12 | Mike | 27836 |
-----------------------------------
So my main select command is: 'SELECT id, title, property_type, owner FROM properties ORDER BY id'
Where for example in owner I have to be able to select both parameters the ownername and phone
Which is dumping the different ID's but I wold like to modify this query so it selects the values from the different tables based on the id in the main table.
Any help achieving this will be appreciated. And the Select should be realized with a single query.
the Result of the query should be:
id(property table),english(property_type table), ownername (owner table:), phone (owner table).
It looks like this is the query you want:
SELECT
P.id
,PT.english
,O.ownername
,O.phone
FROM property P
JOIN property_type PT
ON PT.id = P.property_type
JOIN owner O
ON O.id = P.owner
SQLFiddle Here
select p.id, p.title, p.property_type, o.ownername, o.phone, pt.english
from property p join property_type pt
on p.property_type = pt.id
join owner o
on o.id = p.owner
You would just join the tables. Add more columns if needed.
I would love to get some help with this. I'm using php and MySQL to build a website. I currently have 3 tables, I'll include less in the examples. Basically I have a users table, a groups table and a grouplink table. What I have is the uid from the users table.
How should I go about it in php so I could, let's say: match users-uid to grouplink-uid, get the grouplink-gid it matches with, match grouplink-gid to groups-gid and return groups-grpname? And goes on a while loop so all group names the user is associated with are displayed.
Thanks in advance to those who will be willing to extend a hand.
users
-------
| uid |
-------
| 1 |
-------
groups
---------------
| gid |grpname|
---------------
| 1 | grp1 |
---------------
| 2 | grp2 |
---------------
grouplink
-------------------
| glid| uid | gid |
-------------------
| 1 | 1 | 1 |
-------------------
| 2 | 1 | 2 |
-------------------
uid is fk to uid in users while gid is fk to gid in groups
That's just a simple 2-way join query:
SELECT users.uid, groups.gid, groups.grpname
FROM users
INNER JOIN grouplink ON users.uid = grouplink.uid
INNER JOIN groups ON grouplink.gid = groups.gid
the actual retrieval of a joined query result is no different than a single table query - you've just got more fields to deal with.
The SQL query that will get you what you're looking for goes something like this (assuming no null values in the grouplink table):
SELECT u.uid, g.gid, g.grpname
FROM users u
JOIN grouplink gl ON u.uid = gl.uid
JOIN groups g ON gl.gid = g.gid
Here is one way:
SELECT users.uid, groups.gid, groups.grpname
FROM users u, groups g, grouplink gl
WHERE g.id = gl.gid
AND gl.uid = u.uid
When the user-id is in the variable $iUserId you could query following sql string:
$sSql = "SELECT groups.`grpname` FROM groups
INNER JOIN grouplink ON groups.`gid` = grouplink.`gid`
WHERE grouplink.`uid` = '" . intval($iUserId) . "'";
$rRes = mysql_query($sSql);
$aGroups = array();
while (($aRow = mysql_fetch_array($rRes)) !== false) {
$aGroups[] = $aRow['grpname'];
}
Now all groups associated with the user are in the array $aGroups.
I'm using two tables. First (friendlist), which contains users who are on the list of friends and the other table (members) that contains the basic data of the users.
Friendlist looks like:
id | myid | date | user
-----------------------------------------
001 | 50624 | 2010-01-01 | 32009
002 | 41009 | 2010-05-05 | 50624
003 | 50624 | 2010-03-02 | 23007
The column "myid" contains members who added other users (those from column "user") to their frindlist. I want to get the list of all users that I have added to list and those who add me to their friendlist.
In this example, if my id is 50624, the list would look like:
| allfriends |
---------------
32009
41009
23007
Then I need to check all users from "allfriend" list with data from the table "members". I want to get only the users with status 1.
The members table looks like:
id | status | photo
--------------------------------
32009 | 0 | 1.jpg
41009 | 1 | 2.jpg
23007 | 1 | 3.jpg
How this mysql query should look like?
Thanks for any help.
SELECT id, status, photo FROM members WHERE id IN(
SELECT user FROM friendlist WHERE myid = 50624
UNION ALL
SELECT myid FROM friendlist WHERE user = 50624
) AND status = 1
SELECT user AS allfriends
FROM friendlist
INNER JOIN members
ON user = id
WHERE myid = 50624 AND status = 1
UNION
SELECT myid AS allfriends
FROM friendlist
INNER JOIN members
ON user = id
WHERE user = 50624 AND status = 1`
my friendlist:
select
members.*
from friendlist
inner join members
on members.id=friendlist.user
where friendlist.myid=50624 and members.status=1;
people who are friend with me
select
members.*
from friendlist
inner join members
on members.id=friendlist.myid
where friendlist.user=50624 and members.status=1;
To combine both results, use union
select
members.*
from friendlist
inner join members
on members.id=friendlist.user
where friendlist.myid=50624 and members.status=1
union
select
members.*
from friendlist
inner join members
on members.id=friendlist.myid
where friendlist.user=50624 and members.status=1;
Something like this should work:
SELECT ALLFRIENDS.id, MEMBERS.status FROM members MEMBERS
JOIN (SELECT id FROM (
SELECT myid AS id FROM friendlist WHERE user=50624
UNION
SELECT user AS id FROM friendlist WHERE myid=50624
) AS tmp
) AS ALLFRIENDS ON ALLFRIENDS.id = MEMBERS.id
WHERE MEMBERS.status = 1;