As the title describes all
we can declare cursor like this
DECLARE cur1 CURSOR FOR SELECT id,data FROM test.t1;
Can we use join query instead off simple query?
$sql = "SELECT logs.full_name, logout.status FROM logs, logout WHERE logs.employee_id = logout.employee_id";
tables -- logs
logout
I'm having error on this. I search join tables in google. And that's what I got. What is wrong with this code?
Hello all , Can any one please give me the query to get the child name and his grand fathers name For eg - if i have a table Relationships in the i have childid and fatherid columns so how will i get the grandfather,
i can easily get the father name by using a join but for grandfather i need to do joins 2 times so can any one help me with this
D.Mahesh
I have the following SQL:
SELECT *
FROM [Database].dbo.[TagsPerItem]
INNER JOIN [Database].dbo.[Tag] ON [Tag].Id = [TagsPerItem].TagId
WHERE [Tag].Name IN ('home', 'car')
and it returns:
Id TagId ItemId ItemTable Id Name SiteId
1 1 1 Content 1 home 1
2 1 2 Content 1 home 1
3 1 3 Content 1 home 1
4 2 4 Content 2 car 1
5 2 5 Content 2 car 1
6 2 12 Content 2 car 1
instead of just two records, which these names are "home" and "car". How can I fix it?
Thanks.
From some of the research I've done on NoSQL, column-oriented databases (like HBase or Cassandra) seem to solve the problem of costly JOIN operations, but I don't get how this approach solves this problem.
Can anyone explain it to me and/or link me to interesting documentation regarding this area?
Thanks
i have 3 tables structure is below
tbl_login
login_id | login_name
1 | keshav
tbl_role
role_id | login_id( refer to tbl_login.login_id)
1 | 1
tbl_stuff
stuff_id | role_id( refer to tbl_role.role_id)
1 | 1
i need data in follow format
stuff_id | login_name
1 | keshav
how to use JOIN to retrive the above data in mysql?
$id=(int)$_GET["id"];
$result = mysql_query("SELECT questionstable.*, categorytable.name
FROM questionstable
INNER JOIN categorytable
ON categorytable.id = questionstable.category
WHERE id=$id");
$row = mysql_fetch_assoc($result);
Warning: mysql_fetch_assoc() expects
parameter 1 to be resource, boolean
given in C:\Program
Files\EasyPHP-5.3.4.0\www\scsoru.php
on line 33
Hi,
I have the following query which returns 2 tuples
SELECT bar_id, bar_name, town_name, bar_telephone, subscription_type_id, type
FROM towns, subscriptiontype, regions, bar
LEFT JOIN barpictures bp ON bar.bar_id = bp.bar_id_fk
WHERE town_id = town_id_fk
AND bar.test_field = 0
AND subscription_type_id = subscription_type_id_fk
AND region_id = region_id_fk
AND (type like 'logo%' OR type IS NULL)
The main difference between the tuples is that one has 'type' = logo and the other tuple has 'type' = logo_large. I need that instead of having two tuples, I need that I have 2 type attributes, one holding the "logo" and the other the "logo_large"
eg
bar_id, bar_name, town_name, bar_telephone, subscription_type_id, type1, type2
is this possible
Why is the ( ) mandatory in the SQL statement
select * from gifts INNER JOIN sentgifts using (giftID);
? The ( ) usually is for specifying grouping of something. But in this case, are we supposed to be able to use 2 or more field names... in the example above, it can be all clear that it is 1 field, is it just that the parser is not made to bypass the ( ) when it is all clear? (such as in the language Ruby).
My knowledge of mysql is not very in depth. If I have two tables that for example look like this:
Table1
Date v1 v2 v3
05/01/2010 26 abc 45
05/02/2010 31 def 25
05/03/2010 50 ghi 46
Table2
Date v1 v2 v3
05/01/2010 42 jkl 15
05/02/2010 28 mno 14
05/03/2010 12 pqr 64
How can I join them in a query by their date and have the sum of table1.v1 and table2.v1 and also have the sum of table1.v3 and table2.v3. V2 should be ignored.
I have a table of basketball leagues, a table af teams and a table of players like this:
LEAGUES
ID | NAME |
------------------
1 | NBA |
2 | ABA |
TEAMS:
ID | NAME | LEAGUE_ID
------------------------------
20 | BULLS | 1
21 | KNICKS | 2
PLAYERS:
ID | TEAM_ID | FIRST_NAME | LAST_NAME |
---------------------------------------------
1 | 21 | John | Starks |
2 | 21 | Patrick | Ewing |
Given a League ID, I would like to retrieve all the players' names and their team ID from all the teams in that league, so I do this:
SELECT t.id AS team_id, p.id AS player_id, p.first_name, p.last_name
FROM teams AS t
JOIN players AS p ON p.team_id = t.id
WHERE t.league_id = 1
which returns:
[0] => stdClass Object
(
[team_id] => 21
[player_id] => 1
[first_name] => John
[last_name] => Starks
)
[1] => stdClass Object
(
[team_id] => 21
[player_id] => 2
[first_name] => Patrick
[last_name] => Ewing
)
+ around 500 more objects...
Since I will use this result to populate a dropdown menu for each team containing each team's list of players, I would like to group my result by team ID, so the loop to create these dropdowns will only have to cycle through each team ID instead of all 500+ players each time.
But when I use the GROUP BY like this:
SELECT t.id AS team_id, p.id AS player_id, p.first_name, p.last_name
FROM teams AS t
JOIN players AS p ON p.team_id = t.id
WHERE t.league_id = 1
GROUP BY t.id
it only returns one player from each team like this, overriding all the other players on the same team because of the use of the same column names.
[0] => stdClass Object
(
[team_id] => 21
[player_id] => 2
[first_name] => Patrick
[last_name] => Ewing
)
[1] => stdClass Object
(
[team_id] => 22
[player_id] => 31
[first_name] => Shawn
[last_name] => Kemp
)
etc...
I would like to return something like this:
[0] => stdClass Object
(
[team_id] => 2
[player_id1] => 1
[first_name1] => John
[last_name1] => Starks
[player_id2] => 2
[first_name2] => Patrick
[last_name2] => Ewing
+10 more players from this team...
)
+25 more teams...
Is it possible somehow?
Based on the following table
ID Date State
-----------------------------
1 06/10/2010 Complete
1 06/04/2010 Pending
2 06/06/2010 Active
2 06/05/2010 Pending
I want the following ouptut
ID Date State
---------------------------
1 06/04/2010 Complete
2 06/05/2010 Active
So date is the earliest one and State is the latest one. I am failing to apply self join on the table to get the output.
Thanks
I am trying to find all deals information along with how many comments they have received. My query
select deals.*,
count(comments.comments_id) as counts
from deals left join comments on
comments.deal_id=deals.deal_id where
cancelled='N'
But now it only shows the deals that have at least one comment. What is the problem?
Hi,
I am tying to join the following 2 queries but I am having duplicated .... it is possible to remove duplacted fro this:
(
SELECT bar_id, bar_name, town_name, bar_telephone,
(subscription_type_id *2) AS subscription_type_id
FROM bar, sportactivitybar, towns, subscriptiontype
WHERE sport_activity_id_fk =14
AND bar_id = bar_id_fk
AND town_id = town_id_fk
AND subscription_type_id = subscription_type_id_fk
)
UNION
(
SELECT bar_id, bar_name, town_name, bar_telephone,
subscription_type_id
FROM bar, towns, subscriptiontype
WHERE town_id = town_id_fk
AND subscription_type_id = subscription_type_id_fk
)
ORDER BY subscription_type_id DESC , RAND( )
Please note that I need to omit those duplicates that will have a lower subscription_type_id
When using a SQL join, is it possible to keep only rows that have a single row for the left table?
For example:
select * from A, B where A.id = B.a_id;
a1 b1
a2 b1
a2 b2
In this case, I want to remove all except the first row, where a single row from A matched exactly 1 row from B.
I'm using MySQL.
Hi,
Anyone know how to do an update with a join (i.e. update on two tables in one query) in Doctrine 1.2?
I spotted something obscure on a forum that hinted that this is not supported in 1.x but it was about as vague as it comes.
Thank you.
Hay All.
I have a list which consists of many lists, here is an example
[
[Obj, Obj, Obj, Obj],
[Obj],
[Obj],
[
[Obj,Obj],
[Obj,Obj,Obj]
]
]
Is there a way to join all these items together as 1 list, so the output will be something like
[Obj,Obj,Obj,Obj,Obj,Obj,Obj,Obj,Obj,Obj,Obj]
Thanks
Hi,
I want to get some results via query simillar to:
SELECT
* FROM
users LEFT JOIN
IF (users.type = '1', 'private','company') AS details ON
users.id = details.user_id WHERE
users.id = 1
Any ideas?
Hello there,
how to join 3 relational tables with the structure:
t1 | id
t2 | id | rating
t3 | source_id | relation
t3 stores the data of a field which t1 and t2 uses both. so source_id field can be t1's id or t2's id.
input : t1 id
output : t2 rating
an example:
**t1**
id |
---------
42 |
**t2**
id | rating
-------------
37 | 9.2
**t3**
id | source_id
--------------
42 | 1
37 | 1
26 | 2
23 | 1
what i want is to get 9.2 output with 42 input.
can you do that in one sql query?
SELECT TotalItems.Total_Items
,TotalItems.No_Items_Present
,ItemsTable.No_Of_Items_Ret
FROM TotalItems
INNER JOIN ItemsTable ON
ItemsTable.Item_Name= '" + DropItemName.SelectedValue + "'"
this is my SQL query what I want is to retrieve two column values corresponding to the item I enter in my dropdown list from one table and the no_of_items_ret from another table satisfying the condition of the dropdownlist
I'm getting all the values corresponding to any item I enter what should i do?
I am trying to write a query that grabs information from one database and joins it to information in a different database.
TableA
idA
valueA
idB
TableB
idB
valueB
The tricky part is that in TableA, idB isn't always defined, so when I do a normal join, I only get results where TableA has a idB value. What I want is to be able to grab all of the information from TableA even if it doesn't have a corresponding idB value.
If I have two tables; Drivers keyed by DriverId and Trips with foreign keys DriverId and CoDriverId, and I want to find all trips where a driver was either the driver or co-driver I could code this in Transact-SQL as
select d.DriverId, t.TripId
from Trips t inner join Drivers d
on t.DriverId = d.DriverId or t.CoDriverId = d.DriverId
How could this be coded as a LINQ query?
if i have queries on multiple tables like:
d = Relations.objects.filter(follow = request.user).filter(date_follow__lt = last_checked)
r = Reply.objects.filter(reply_to = request.user).filter(date_reply__lt = last_checked)
article = New.objects.filter(created_by = request.user)
vote = Vote.objects.filter(voted = article).filter(date__lt = last_checked)
and i want to display the results from all of them ordered by date (i mean not listing all the replies, then all the votes, etc ).
Somehow, i want to 'join all these results', in a single queryset.
Is there possible?
Is this equivalent to a LEFT JOIN?
SELECT DISTINCT a.name, b.name
FROM tableA a,
(SELECT DISTINCT name FROM tableB) as b
It seems as though there is no link between the two tables.
Is there an easier / more efficient way to write this?
Hi, can you help me with sql query?
I have this problem:
I have two tables
"Join" table: Reservation_has_meal
+----------------+
| id_reservation |
| id_meal |
| pieces |
+----------------+
and table with data: Meal
+-------------+
| id_meal |
| name |
+-------------+
Sample data for
Meal:
1 | carrot
2 | potatoe
3 | cucumber
Reservation_has_meal
1 | 2 | 5230
1 | 3 | 1203
How can I get this result for reservation with id_reservation=1:
id_meal | id_Reservation | name | pcs |
--------------------------------------------
1 | 1 | carrot | null|
2 | 1 | potatoe | 5230|
3 | 1 | cucumber | 1203|
--------------------------------------------
And result for id_reservation = 2:
id_meal | id_Reservation | name | pcs |
--------------------------------------------
1 | 2 | carrot | null|
2 | 2 | potatoe | null|
3 | 2 | cucumber | null|
--------------------------------------------
Thanks for advice.