i have 2 table like this
accounts
-------------
id|
username|
password|
email|
date
bans
-------------
id|
accounts|
ip|
admin|
reason|
date|
for example on bans show result like this
5|NULL|0|1|hayo ketauan|2017-01-17 20:05:5
i want bans.accounts and bans.admin show accounts.username
i have query like this
SELECT `accounts`.`username`, `bans`.`ip`, `bans`.`admin`, `bans`.`reason`, `bans`.`date`
FROM `accounts`
INNER JOIN `bans`
ON `accounts`.`id` = `bans`.`account`
and got result
Naufal|NULL|1.1.1.1|1|hayo ketauan|2017-01-17 20:05:5
i want to like this
Naufal|1.1.1.1|frans|hayo ketauan|2017-01-17 20:05:5
I'm confuse about the query.
To get the username of the admin, you need to JOIN to the accounts table a second time using the admin value of the bans table. This should work:
SELECT `a1`.`username`, `bans`.`ip`, `a2`.`username` AS admin, `bans`.`reason`, `bans`.`date`
FROM `accounts` `a1`
INNER JOIN `bans` ON `a1`.`id` = `bans`.`account`
INNER JOIN `accounts` `a2` ON `a2`.`id` = `bans`.`admin`
Related
i have a comment table with this fields
id | user_id | parent_id
and a user table
id | username
how sholud i join this table with itself and user table to get parent comment user name?
SELECT comment.* ,c1.id as child_id,c1.user_id as child_user_id FROM `comment`
LEFT JOIN comment c1 ON c1.parent=comment.id
LEFT JOIN users ON users.id=child_user_id
in first join i get child_user_id which is a user id that i want it's user name But how can I join user table based on child_user_id?
Try something like this:
SELECT *,(
SELECT username FROM user WHERE id = a.parent_id
) parent_username
FROM comment a
JOIN user b on a.user_id = b.id
I have two tables; What I need to do is select comments of a given user. I need cid and heading as results
posts
pid | heading | body | username
1 smth.... smth.. u1
2 smth.... smth.. u2
posts
cid | body | username
1 smth.. u1
2 smth.. u2
I have tried to use JOINS, mostly INNER . But the answer was wrong. Then I tried with a sub query again answers are wrong, but this time its a different answer than before. Now I'm trying to use INNER JOINS with a sub query together. I don't know if thats possible or not.
Some SQL that I have tried; I won't post all since there are too many things I tried.
SELECT `comment_id`, `post`.`post_id`, `friendly_url`, `heading` FROM `post`,`comments` WHERE `post`.`post_id` IN (SELECT `comments`.`post_id` FROM `comments` WHERE `username` = ?)
SELECT `post`.`post_id`, `friendly_url`, `heading` FROM `post`INNER JOIN `comments` ON `post`.`post_id`= `comments`.`post_id` WHERE `post`.`post_id` IN (SELECT `comments`.`post_id` FROM `comments` WHERE `username` = 'chichi')
Per your posted query it looks like there is a relation exists b/w the tables
`post`.`post_id` = `comments`.`post_id`
So you can try using a INNER JOIN like
SELECT c.`comment_id`, p.`post_id`, c.`friendly_url`, c.`heading`
FROM `post` p JOIN `comments` c ON p.`post_id` = c.`post_id`
WHERE `username` = 'u1'
I have 3 tables in a database that have similar values and the same table structure. I am trying to get the number of occurrences of each value by unique user.
DB Structure
View on SQLFiddle
TABLE_1
user | value | id
TABLE_2
user | value | id
TABLE_3
user | value | id
I can run the following MySQL command to retrieve the desired results on 1 table at a time.
SELECT value,COUNT(*) as count FROM TABLE_1 GROUP BY value ORDER BY count DESC;
I need to run this command across the three tables at once in order to retrieve the unique occurrences of "value" among a list of "users" that contains numerous duplicates.
Given your comments, since you want to remove duplicates, use UNION to combine the data from the tables together:
SELECT value, COUNT(*) as count
FROM (
SELECT user, value, id
FROM TABLE_1
UNION
SELECT user, value, id
FROM TABLE_2
UNION
SELECT user, value, id
FROM TABLE_3 ) t
GROUP BY value
ORDER BY count DESC;
Updated Fiddle
You need to use UNION -
SELECT value, COUNT(*) as count
FROM (
SELECT user, value, id
FROM TABLE_1
UNION
SELECT user, value, id
FROM TABLE_2
UNION
SELECT user, value, id
FROM TABLE_3 ) tables
GROUP BY value
ORDER BY count DESC;
Output -
+-------+-----+
|car |8 |
|boat |4 |
|truck |3 |
|house |2 |
|skates |1 |
|bike |1 |
+-------+-----+
to go along with the comments this is what I would recommend you do.
setup:
CREATE TABLE members (
id int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
username varchar(255),
);
INSERT INTO members (username)
SELECT DISTINCT user FROM table1
UNION SELECT DISTINCT user FROM table2
UNION SELECT DISTINCT user FROM table3;
altering:
ALTER table1
ADD COLUMN user_id INT(10)
ADD INDEX `user_id` (`user_id`);
ALTER table2
ADD COLUMN user_id INT(10)
ADD INDEX `user_id` (`user_id`);
ALTER table3
ADD COLUMN user_id INT(10)
ADD INDEX `user_id` (`user_id`);
updating:
UPDATE table1 t,
JOIN members m ON m.username = t.username
SET t.user_id = m.id;
UPDATE table2 t,
JOIN members m ON m.username = t.username
SET t.user_id = m.id;
UPDATE table3 t,
JOIN members m ON m.username = t.username
SET t.user_id = m.id;
removing non normalized data
ALTER table1
DROP user;
ALTER table2
DROP user;
ALTER table3
DROP user;
now you can also set up foreign key contstraints on the user_id and id columns if you would like.
but to query a total count you can just join the tables.. make sure you add an index on each of the id fields so it will join properly.
SELECT your_stuff
FROM members m
LEFT JOIN table1 t1 ON t1.user_id = m.id
LEFT JOIN table2 t2 ON t2.user_id = m.id
LEFT JOIN table3 t3 ON t3.user_id = m.id
I have two tables in a database that looks like this
members table1
id | memberid | name | password
-------------------------------
Journal table2
id | memberid | title | desc
-----------------------------
i want to select all members from table one, joining with the result journal_count of each member in table 2. Am trying to count the number of times each memberid appears in table2
Am using mysql and php, can someone help me with this query
thanks
select m.memberid,m.name,count(j.memberid) as total
from members as m
left join journal as j
on m.memberid = j.memberid
group by m.memberid
P.S. If your table has a field named desc, beware that this is a reserved word and it would be better to rename it. Otherwise you'll always have to put it within backticks.
select m.memberid,m.name,count(j.memberid) as total
from members as m
left join journal as j
on m.memberid = j.memberid
where m.memberid = $memberid
group by m.memberid
select members.name, count(journal.memberid) as journal_count from members, journal where members.memberid = journal.memberid group by journal.memberid
i know there is heaps of this same question but im having trouble making it work on multiple table joins. i have 3 tables
userinfo
user_id | firstname | lastname
subnumbers
subnumber | fkuserid
transaction
transid | fksubnumber | expires | transdate
heres the part i have working
SELECT *
FROM (SELECT *
FROM subtransactions
WHERE Expires < now()
ORDER BY NewSubTrans DESC) AS s
GROUP BY FKSubNum
What i am trying to do is select all users with a subscription that is expired. to determine if a user is expired i pull the last transaction from the transaction table with thier sub number, the fk fields are the links to the other tables.
From your description, I don't think you need any group at all. Try it with just these two joins:
SELECT user_id, firstname, lastname
FROM
userinfo
JOIN subnumbers ON userinfo.user_id = subnumbers.fkuserid
JOIN transaction ON subnumbers.subnumber = transaction.fksubnumber
WHERE transaction.expires < NOW()
EDIT If it returns multiple rows as suggested in comments below, use DISTINCT in the select list:
SELECT DISTINCT user_id, firstname, lastname
-- etc...
This will get all user_ids
select distinct(user_id) from userinfo
join subnumbers on userinfo.user_id = subnumbers.fkuserid
join transaction on transaction.fksubnumber = subnumbers.subnumber
where transaction.expires < NOW()
To get full user data
select * from user where user_id in
(
select distinct(user_id) from userinfo
join subnumbers on userinfo.user_id = subnumbers.fkuserid
join transaction on transaction.fksubnumber = subnumbers.subnumber
where transaction.expires < NOW()
)