mysql query query - mysql

basically i need to write a query for mysql, but i have no experience in this and i cant find good tutorials on the old tinternet.
i have a table called rels
with columns "hosd_id" "linkedhost_id" "text link"
and a table called hostlist
with columns "id" "hostname"
all i am trying to achieve is a query which outputs the "hostname" and "linked_id" when "host_id" is equal to "id"
any help or pointers on syntax or code would be helpfull, or even a good mysql query
guide

I always thought w3schools and Tizag tutorials were pretty good for beginners...
http://www.w3schools.com/sql/default.asp
http://www.tizag.com/mysqlTutorial/

Try:
SELECT hostname, linkedhost_id
FROM rels, hostlist
WHERE host_id = id;

This should do the trick;
SELECT hostname, linked_id FROM hostlist, rels WHERE rels.host_id = hostlist.id

Try:
SELECT h.hostname, r.linkedhost_id
FROM rels r
INNER JOIN hostlist h ON h.id = r.hosd_id
The MySQL documentation has a section on SQL Syntax that is a good start for learning how to write SQL queries.

Try this:
select h.hostname, r.linkedhost_id from rels r inner join hostlist h on
r.hosd_id = h.id where r.host_id = hostlist.id
Finally, have a look at basics of mysql.

Everyone has answered this question correctly, but i also want to post an answer to this.
Here's mine:
SELECT hostlist.hostname, rels.linkedhost_id
FROM rels
INNER JOIN hostlist ON (hostlist.id = rels.host_id)
WHERE rels.host_id = hostlist.id;

Related

how to write this query in correct syntax?

SELECT collegename(SELECT allotement.collegename,dean.id
FROM dean,allotement
WHERE allotement.city=dean.city
&&dean.collegename<>allotement.collegename
&&dean.id<>allotement.id)as t WHERE id=1
SELECT collegename from (
SELECT allotement.collegename, dean.id
FROM dean,allotement WHERE allotement.city=dean.city
and dean.collegename<>allotement.collegename
and dean.id<>allotement.id)
as t WHERE id=1
A few points to note here:
Treat sub-query as a table source from which you are retrieving the data. Thus, you need a from in the first line.
&& doesn't work in SQL. You have to write and instead.
In your case, writing as t is optional.
You can actually go through a pretty good link which I generally use to follow mySQL syntax, as it's a bit confusing, considering the fact that different SQL databases have a slight variation in syntax and functions available.
You can refer to the official mySQL docs here as well, if in case required.
TRY THIS: We can simply achieve that in following simple way even we don't need sub query for that:
SELECT a.collegename, d.id
FROM dean AS d
INNER JOIN allotement AS a ON a.city = d.city
AND d.collegename <> a.collegename
AND d.id <> a.id
WHERE d.id = 1

Nesting COUNT in statement with JOIN

Really trying to figure out, why SQL query doesnt go through. I assume the structure is a bit wrong, but cant figure out where exactly. The references to tables are all correct.
SELECT tap_questionnaires.id,
tap_questionnaires.NAME,
tap_questionnaires.active,
tap_useranswers_ip.questionnaire_id,
Count(tap_useranswers_ip.ip)
FROM tap_questionnaires
LEFT JOIN tap_useranswers_ip
ON tap_questionnaires.id = tap_useranswers_ip.questionnaire_id
WHERE author_email = admin#admin.com
If you use count you need to use group by for the other columns in your select clause.
SELECT TAP_questionnaires.id, TAP_questionnaires.name, TAP_questionnaires.active, TAP_useranswers_ip.questionnaire_id, COUNT(TAP_useranswers_ip.ip) FROM TAP_questionnaires LEFT JOIN TAP_useranswers_ip on TAP_questionnaires.id=TAP_useranswers_ip.questionnaire_id WHERE author_email="admin#admin.com"
group by TAP_questionnaires.id, TAP_questionnaires.active
I think TAP_questionnaires.name it's not necessary because I suppose it depends on TAP_questionnaires.id. TAP_useranswers_ip.questionnaire_id is the same value as TAP_questionnaires.id
Hope that helps!
I think this version is clearer:
SELECT q.id, q.name, q.active, COUNT(a.ip)
FROM TAP_questionnaires q LEFT JOIN
TAP_useranswers_ip a
ON on q.id = a.questionnaire_id
WHERE author_email = 'admin#admin.com'
GROUP BY q.id, q.name, q.active;
Notes:
You need a GROUP BY.
You need single quotes around the string constant.
Table aliases make the query easier to write and to read.
There is no reason to include a.questionnaire_id. You already have q.id.

Joomla 2.5 joining 3 tables

I am trying to join 3 tables and store the result.
The problem is I simply cannot seem to deal with it. Hours of Googling and searching led to nothing.
Here is the query:
$db = JFactory::getDBO();
$item_kit = array();
$q = "SELECT p.virtuemart_product_id,
pr.product_price,
pr.virtuemard_product_id,
pb.group_id
FROM #_virtuemart_products p
INNER JOIN #_virtuemart_product_prices pr ON p.virtuemart_product_id = pr.virtuemart_product_id,
INNER JOIN #_pb_group_vm_prod_xref pb ON pb.vm_product_id = pr.virtuemart_product_id";
$db->setQuery($q);
$item_kit = $db->loadObjectList();
return $item_kit;
Please notice that I am using the ID as the common element in all tables.
After all this nonsense occurs it should so something like:
if(!empty($item_kit['group_id'])){
echo 'It works';
}
Debugging resulted in discovering an SQL error near 'INNER JOIN #_virtuemart_product_prices pr ON p.virtuemart_product_id = pr.virtu...'
Can you please help me? Or, at least, point out where am I doing stupidly wrong...
I know it's something small and easy, but I can't see it...
Found the problem!
pr.virtuemard_product_id to pr.virtuemart_product_id
and I had to manually type in the database table prefix.
What silly of me. Thank you all for your time reading this question.
Regards,
Vlad
Remove the comma at the end of the first inner join line
INNER JOIN #_virtuemart_product_prices ... pr.virtuemart_product_id,
it should become
INNER JOIN #_virtuemart_product_prices ... pr.virtuemart_product_id

SQL Sub-select statements filter using where clause

I am in the process of building a dashboard and need to extract some data from a somewhat complex schema.
I have a select statement (see below) that I am using to extract information, but need to conduct some filtering on part of the select statement, and I'm struggling.
select distinct j.id 'Job_Id'
, js.outcome 'Outcome'
,(select string from property where parent_sheet_id = ps.id and name= 'Build_Type') as 'Build_Type'
from job j, job_step js, property_sheet ps, property p
where j.id = js.job_id
and ps.entity_id=js.id
and ps.id=p.parent_sheet_id
and ps.entity_type='step'
and p.name = 'Id'
group by j.id
order by j.id desc;
I am sure that there is a better way of doing this query, and I would appreciate any other suggestions, but I am mostly attempting to place a filter on the nested select statement which has an alias of "Build_Type", but when I try it appears not to work. I've read some blogs that this is not possible, so I am a little stuck.
Any help would be much appreciated.
Thanks.
select
ps.id,
Build_Type.string
from
property_sheet as ps
left join
property as Build_Type
on ps.id = Build_Type.parent_sheet_id and Build_Type.name = 'Build_Type'
where Build_Type....

LINQ To SQL "Group By"

I wonder if someone can help me. I want to replicate the following SQL query using LINQ in VB.Net.I'm a little unclear on how to do subqueries / aggregates.
Thanks
SELECT *
FROM Server S
INNER JOIN ServerHDD H
ON S.Server_ID = H.Server_ID
INNER JOIN (SELECT MAX(ServerHDD_ID) AS ServerHDD_ID
FROM ServerHDD
GROUP BY Server_ID, Letter) Filter
ON H.ServerHDD_ID = Filter.ServerHDD_ID
ORDER BY S.Hostname, H.Letter
Got this as below in C# => need VB.Net Conversion please.
from S in SERVER
join H in SERVERHDD on S.Server_ID equals H.Server_ID
join FILTER in
(from s in SERVERHDD group s
by new {s.Server_ID, s.Letter}
into groupedServerHDD select new
{
SERVERHDD_ID = groupedServer.Sum(gS=>gS.ServerHDD_ID)
}
)
on H.ServerHDD_ID equals FILTER.SERVERHDD_ID
orderby S.Hostname, H.Letter
select S
This is my most favorite page regarding this topic. I love LINQ to SQL (and wish they intended to continue support for it over Entity Framework...) http://msdn.microsoft.com/en-us/vbasic/bb688085.aspx. On this page you will find all the answers to your querying needs. It is hard to format the query here without something to test it against!
Your inner joins go away with the simple join syntax of LtS. You can either say .Max() on your inner select or Max(pseudo functoid here) like this:
From p2 In g _
Where p2.UnitPrice = g.Max(Function(p3) p3.UnitPrice) _
Select p2
There are a couple of LINQ learning tools out there that are pretty cool.
This one is my favourite... LinqPad. But you might also want to check out Linqer.
You should be able to paste your code into one of them apps and it will show you how its converted. Hope they come in handy :)