MySQL Select Column if Column in another table is zero - mysql

My issue is mostly that im not sure how to word what I need in a search.
mysql> SELECT * FROM `accounts`;
+----+--------+---------+----------+
| id | status | oplevel | username |
+----+--------+---------+----------+
| 10 | 1 | 0 | root |
| 11 | 1 | 1 | Xylex |
| 12 | 1 | 16 | Anubis |
| 13 | 0 | 16 | Kami |
| 14 | 1 | 16 | Zorn |
+----+--------+---------+----------+
mysql> SELECT * FROM `networks`;
+-----------+-----------+-------------+-----------------+
| networkid | accountid | networkname | serveraddress |
+-----------+-----------+-------------+-----------------+
| 1 | 10 | Fakenet | irc.fakenet.org |
| 2 | 10 | Undernet | irc.under.net |
| 3 | 12 | Takenet | irc.takenet.com |
| 4 | 13 | Examplenet | irc.example.org |
+-----------+-----------+-------------+-----------------+
I have this accounts table and this networks table
I need a single query that will return the address of the IRC network, ONLY IF the corresponding account id status column is 1 (enabled)
Ive been searching for hours.
What Do?

looks like a simple join to me:
select serveraddress from accounts join networks on id=accountid and id=YOUR_ACCOUNT_ID_HERE
Or
select serveraddress from networks where accountid=YOUR_ID and exists(select * from accounts where id=accountid and status)
Many ways to go about this..

select serveraddress from networks, accounts where networks.accountid = accounts.id and accounts.satus=1;
Is it what you're looking for?

I've updated the query to place account id.
select acc.id, acc.username, nw.serveraddress from accounts acc join networks nw on acc.id = nw.accountid where acc.status == 1 and acc.id=XXX

Related

How to update a column with the number of rows that have a matching column pair?

I have a table called related_clues which lists the id's of pairs of clues which are related
| id | clue_id | related_clue_id | relatedness |
+----+---------+-----------------+-------------+
| 1 | 1 | 232 | 1 |
| 2 | 1 | 306 | 1 |
| 3 | 1 | 458 | 1 |
| 4 | 2 | 620 | 1 |
| 5 | 2 | 72 | 1 |
| 6 | 3 | 212 | 1 |
| 7 | 3 | 232 | 1 |
| 8 | 3 | 412 | 1 |
| 9 | 3 | 300 | 1 |
+----+---------+-----------------+-------------+
Eventually after a while we may reach two id's such as:
+--------+---------+-----------------+-------------+
| id | clue_id | related_clue_id | relatedness |
+--------+---------+-----------------+-------------+
| 121267 | 1636 | 38 | 1 |
| 121331 | 1636 | 38 | 1 |
+--------+---------+-----------------+-------------+
So in this case, for two distinct id values, we have the same (clue_id, related_clue_id) pair
In this case I would like the relatedness value to be updated to 2, signalling that there are two examples of this (clue_id, related_clue_id) pair. Like so:
+--------+---------+-----------------+-------------+
| id | clue_id | related_clue_id | relatedness |
+--------+---------+-----------------+-------------+
| 121267 | 1636 | 38 | 2 |
| 121331 | 1636 | 38 | 2 |
+--------+---------+-----------------+-------------+
So essentially I would like to run some SQL that sets the relatedness value to the number of times a (clue_id, related_clue_id) pair appears.
When I have no relatedness column present, and I simply run the SQL:
SELECT id, clue_id, related_clue_id, COUNT(*) AS relatedness
FROM `related_clues`
GROUP BY clue_id, related_clue_id
It gives me the required result, but of course this doesn't store the relatedness column, it simply shows the column if I run this select. So how do I permanently have this relatedness column?
You could use a update with join
Update related_clues a
INNER JOIN (
SELECT clue_id, related_clue_id, COUNT(*) AS relatedness
FROM `related_clues`
group by clue_id, related_clue_id
having count(*) = 2
) t on t.clue_id = a.clue_id
and t.related_clue_id = a.related_clue_id
set a.relatedness = t.relatedness
I would approach this as an update/join but filter out rows that don't need to be updated:
update related_clues rc join
(select clue_id, related_clue_id, COUNT(*) AS cnt
from `related_clues`
group by clue_id, related_clue_id
) t
on t.clue_id = rc.clue_id and
t.related_clue_id = rc.related_clue_id
set rc.relatedness = t.relatedness
where rc.relatedness <> t.relatedness;

How to access a row on a table based on a value from another table?

I am doing a mini project using MySQL. I came with the following problem:
I created 2 tables, student and book with 6 and 5 columns respectively.
mysql> select * from book;
+--------+------+------------+---------+------+
| bookid | Name | Authorname | edition | cost |
+--------+------+------------+---------+------+
| cc12 | dbms | guna | 5 | 500 |
| cc34 | CA | fasil | 5 | 600 |
| cs113 | OS | rohan | 3 | 300 |
| cs12 | AI | ganesh | 2 | 1000 |
| cs343 | c# | jackesh | 4 | 300 |
+--------+------+------------+---------+------+
5 rows in set (0.00 sec)
mysql> select * from studentbook;
+-----+--------+-----------+
| Sno | bookid | Studid |
+-----+--------+-----------+
| 1 | cc12 | 14vec1088 |
| 2 | cs113 | 14vec1099 |
| 3 | cc34 | 14vec1132 |
| 4 | cs343 | 14vec2011 |
| 5 | cs12 | 14vec100 |
+-----+--------+-----------+
5 rows in set (0.00 sec)
Now, when I enter any of the studid mentioned in the table studentbook (This is performed by PHP in the backend) it should display the details of book associated with the respective studid from the table book.
How can I perform the above using MySQL Query?
This might be work.
SELECT * FROM book
WHERE bookid IN
(SELECT bookid FROM studentbook
WHERE studid = "[Id of which you want book]");
This should get you what you need.
SELECT b.*
FROM book b
INNER JOIN studentbook sb on b.bookid = sb.bookid
WHERE sb.Studid = [your id]

sql select query from two different tables WHERE active=1

i try select all columns from two different tables WHERE active = 1
i have 2 tables table_pro and table_basic,
sql:"select * from table_basic,table.name";
and 2 condition:
WHERE active = 1
WHERE table_pro.id = table_basic.name.id
how to make it correctly
Here is table_pro
+----+--------+---------+-----------+
| id | people | rooms | active |
+----+--------+---------+-----------+
| 1 | 5 | 10 | 0 |
| 2 | 12 | 17 | 0 |
| 3 | 21 | 38 | 1 |
+----+--------+---------+-----------+
Here is table_basic
+---------+-------+---------+------------+----------+
| name_id | name | balance | title | time |
+---------+-------+---------+------------+----------+
| 1 |shop | 100 | failed | 15:10:20 |
| 2 |factory| 75 | error | 15:10:20 |
| 3 |studio | 25 | timed_out | 15:10:20 |
+---------+-------+---------+------------+----------+
I'd like to have this output result only rows (from of all columns) with status active = 1
+-----+-------+----- --+--------+-------+----------+---------+--------+
| id | people| rooms | name |balance| title | time | active |
+-----+-------+--------+--------+-------+----------+---------+--------+
| 3 | 21 | 38 | studio |25 | timed_out| 15:10:20| 1 |
+-----+-------+--------+--------+-------+----------+---------+--------+
Thanks
SELECT A.id, A.people, A.rooms, B.name, B.balance, B.title, B.time, A.active
FROM
table_pro AS A
JOIN
table_basic AS B
ON
A.id = B.name_id
WHERE
A.id = 3
SELECT table_pro.*, table_basic.*
FROM table_pro
INNER JOIN table_basic
ON table_basic.name_id = table_pro.id
WHERE table_pro.active = 1

Big Mysql Query (Join counts in the result and modify a string)

I am brand new to mysql so please excuse my level of knowledge here, and feel free to direct me in a better direction if what I am doing is out of date.
I am pulling information from a database to fill in a php page.
My tables:
Server:
|ServerID (int) | ArticleID (int) | locationID (int) |
| 1 | 46 | 55 |
| 2 | 11 | 81 |
| 3 | 81 | 46 |
| 4 | 55 | 11 |
| 5 | 81 | 99 |
| 5 | 11 | 52 |
Article:
|ArticleID (int) | Name (varchar) | Typ (int) |
| 46 | domain | 0 |
| 81 | root-server | 1 |
| 55 | vserver | 2 |
| 11 | root-server2 | 1 |
Location:
|LocationID (int) | location (varchar) |
| 46 | 1-5-15-2 |
| 81 | 1-5-14-2 |
| 55 | 2-25-1-9 |
| 11 | 21-2-5-8 |
| 99 | 17-2-5-8 |
| 52 | 1-8-5-8 |
Result:
|location (int) | name (varchar) | count (int) |
| 1 | root-server | 1 |
| 1 | root-server2 | 2 |
| 17 | root-server | 1 |
The location in the result is the first number block of the location in the location table (1-5-15-2 -> 1, 1-8-5-8 -> 1, 21-2-5-8 -> 21, 17-2-5-8 -> 17).
The count is the sum of all servers with the same name and the same first location block.
Do anyone think its possible to get this result in only one query?
Thanks for any answer!
Check this
SELECT SUBSTRING_INDEX(location, '-', 1) as LID,Article.Name,Count(*) as Count
from Location join Server
on Server.locationID=Location.locationID
join Article on Article.ArticleID=Server.ArticleID
group by LID,Article.ArticleID ;
DEMO
Please give this a shot:
SELECT
s.locationID as id, a.name, count(*) as count
FROM
`Server` s
LEFT JOIN
`Article` a ON s.ArticleID = a.ArticleID
GROUP BY s.locationID, a.name
something like this should work
select
s.location_id as location, a.name, count(location) as count
from
server as s, article as a
where
s.articleID = a.articleID
group by location, a.name

Joining 3 tables

First off, sorry if this is a near enough duplicate. I've found this question, which nearly does what I want, but I couldn't wrap my head around how to alter it to my needs.
I've got these 3 tables:
cs_Accounts:
+----+-----------------------------+-------------+
| id | email | username |
+----+-----------------------------+-------------+
| 63 | jamasawaffles#googlil.com | jamwaffles2 |
| 64 | jamwghghhfles#goomail.com | jamwaffles3 |
| 65 | dhenddfggdfgetal-pipdfg.com | dhendu9411 |
| 60 | jwapldfgddfgfffles.co.uk | jamwaffles |
+----+-----------------------------+-------------+
cs_Groups:
+----+-----------+------------+-------------+
| id | low_limit | high_limit | name |
+----+-----------+------------+-------------+
| 1 | 0 | 0 | admin |
| 2 | 1 | 50 | developer |
| 3 | 76 | 100 | reviewer |
| 4 | 51 | 75 | beta tester |
| 5 | 1 | 50 | contributor |
+----+-----------+------------+-------------+
cs_Permissions:
+----+---------+----------+
| id | user_id | group_id |
+----+---------+----------+
| 4 | 60 | 4 |
| 3 | 60 | 1 |
| 5 | 60 | 2 |
| 6 | 62 | 1 |
| 7 | 62 | 3 |
+----+---------+----------+
I've been wrestling with a 3 way join for hours now, and I can't get the results I want. I'm looking for this behaviour: a row will be returned for every user from cs_Accounts where there is a row in cs_Permissions that contains their ID and the ID of a group from cs_Groups, as well as the group with the group_id has a high_lmiit and low_limit in a range I can specify.
Using the data in the tables above, we might end up with something like this:
email username cs_Groups.name
----------------------------------------------------------
jwapldfgddfgfffles.co.uk jamwaffles admin
jwapldfgddfgfffles.co.uk jamwaffles developer
jwapldfgddfgfffles.co.uk jamwaffles beta tester
dhenddfggdfgetal-pipdfg.com dhendu9411 admin
dhenddfggdfgetal-pipdfg.com dhendu9411 reviewer
There is an extra condition, however. This condition is where rows are only selected if the group the user belongs to has a high_limit and low_limit with values I can specify using a WHERE clause. As you can see, the table above only contains users with rows in the permissions table.
This feels a lot like homework but with a name like James I'm always willing to help.
select a.email,a.username,g.name
from cs_Accounts a
inner join cs_Permissions p on p.user_id = a.id
inner join cs_Groups g on g.id = p.Group_id
where g.low_limit > 70
and g.high_limt < 120
This is the query
SELECT ac.email, ac.username, gr.name
FROM cs_Accounts AS ac
LEFT JOIN cs_Permissions AS per ON per.user_id = ac.id
INNER JOIN cs_Groups AS gr ON per.user_id = gr.id
You can add a WHERE clause to this query if you want