Search Results

Search found 7706 results on 309 pages for 'inner join'.

Page 12/309 | < Previous Page | 8 9 10 11 12 13 14 15 16 17 18 19  | Next Page >

  • Can I join two tables whereby the joined table is sorted by a certain column?

    - by Ferdy
    I'm not much of a database guru so I need some help on a query I'm working on. In my photo community project I want to richly visualize tags by not only showing the tag name and counter (# of images inside them), I also want to show a thumb of the most popular image inside the tag (most karma). The table setup is as follow: Image table holds basic image metadata, important is the karma field Imagefile table holds multiple entries per image, one for each format Tag table holds tag definitions Tag_map table maps tags to images In my usual trial and error query authoring I have come this far: SELECT * FROM (SELECT tag.name, tag.id, COUNT(tag_map.tag_id) as cnt FROM tag INNER JOIN tag_map ON (tag.id = tag_map.tag_id) INNER JOIN image ON tag_map.image_id = image.id INNER JOIN imagefile on image.id = imagefile.image_id WHERE imagefile.type = 'smallthumb' GROUP BY tag.name ORDER BY cnt DESC) as T1 WHERE cnt > 0 ORDER BY cnt DESC [column clause of inner query snipped for the sake of simplicity] This query gives me somewhat what I need. The outer query makes sure that only tags are returned for which there is at least 1 image. The inner query returns the tag details, such as its name, count (# of images) and the thumb. In addition, I can sort the inner query as I want (by most images, alphabetically, most recent, etc) So far so good. The problem however is that this query does not match the most popular image (most karma) of the tag, it seems to always take the most recent one in the tag. How can I make sure that the most popular image is matched with the tag?

    Read the article

  • CakePHP: 2-level JOIN with one Query

    - by Daniel Magliola
    I have the following models in CakePHP: A Deposit belongs to an Account An Account belongs to a Customer I want to have a list of Deposits, and I need to show the name of the customer (so I have to join through the Customer). I also need to paginate this list. If I set Deposit->recursive = 2, I can get the Customer, however, CakePHP runs one query joining Deposit and Account, and then runs one query per each Deposit, to get the Customer. How can I make it get both models with only one query? I tried this, but it didn't work: $this->paginate = array('joins' => array( array( 'table' => 'customers', 'alias' => 'AccountCustomer', 'type' => 'inner', 'foreignKey' => false, 'conditions' => array('Account.customer_id = AccountCustomer.id') ) )); Any ideas? Thanks! Daniel

    Read the article

  • Help with a Join in Rails 3

    - by Adam Albrecht
    I have the following models: class Event < ActiveRecord::Base has_many :action_items end class ActionItem < ActiveRecord::Base belongs_to :event belongs_to :action_item_type end class ActionItemType < ActiveRecord::Base has_many :action_items end And what I want to do is, for a given event, find all the action items that have an action item type with a name of "foo" (for example). So I think the SQL would go something like this: SELECT * FROM action_items a INNER JOIN action_item_types t ON a.action_item_type_id = t.id WHERE a.event_id = 1 AND t.name = "foo" Can anybody help me translate this into a nice active record query? (Rails 3 - Arel) Thanks!

    Read the article

  • Join Query Help

    - by John
    Hello, The query below works well. It pulls data from two MySQL tables, "submission" and "login." I would like to also pull data from a third table called "comment" in the same database. The table "comment" has the following fields: commentid, loginid, submissionid, comment, datecommented Two of the fields in the table "login" are called "loginid" and "username." In the query below, I would like to count all "commentid" in "comment" where "loginid" equals the "loginid" in "login" where "username" equals "$profile." How can I do this? Thanks in advance, John $sqlStr1 = "SELECT l.username, l.loginid, s.loginid, s.submissionid, s.title, s.url, s.datesubmitted, s.displayurl, l.created, count(s.submissionid) countSubmissions FROM submission AS s INNER JOIN login AS l ON s.loginid = l.loginid WHERE l.username = '$profile'";

    Read the article

  • Scalable way of doing self join with many to many table

    - by johnathan
    I have a table structure like the following: user id name profile_stat id name profile_stat_value id name user_profile user_id profile_stat_id profile_stat_value_id My question is: How do I evaluate a query where I want to find all users with profile_stat_id and profile_stat_value_id for many stats? I've tried doing an inner self join, but that quickly gets crazy when searching for many stats. I've also tried doing a count on the actual user_profile table, and that's much better, but still slow. Is there some magic I'm missing? I have about 10 million rows in the user_profile table and want the query to take no longer than a few seconds. Is that possible?

    Read the article

  • Nhibernate Criteria Query with Join

    - by John Peters
    I am looking to do the following using an NHibernate Criteria Query I have "Product"s which has 0 to Many "Media"s A product can be associated with 1 to Many ProductCategories These use a table in the middled to create the join ProductCategories Id Title ProductsProductCategories ProductCategoryId ProductId Products Id Title ProductMedias ProductId MediaId Medias Id MediaType I need to implement a criteria query to return All Products in a ProductCategory and the top 1 associated Media or no media if none exists. So although for example a "T Shirt" may have 10 Medias associated, my result should be something similar to this Product.Id Product.Title MediaId 1 T Shirt 21 2 Shoes Null 3 Hat 43 I have tried the following solutions using JoinType.LeftOuterJoin 1) productCriteria.SetResultTransformer(Transformers.DistinctRootEntity); This hasnt worked as the transform is done code side and as I have .SetFirstResult() and .SetMaxResults() for paging purposes it wont work. 2) .SetProjection( Projections.Distinct( Projections.ProjectionList() .Add(Projections.Alias(Projections.Property("Id"), "Id")) ... .SetResultTransformer(Transformers.AliasToBean()); This hasn't worked as I cannot seem to populate a value for Medias.Id in the projections. (Similar to http://stackoverflow.com/questions/1036116/nhibernate-criteria-api-projections) Any help would be greatly appreciated

    Read the article

  • How do I join these queries?

    - by GregoryD
    query1: SELECT category.id, category.name, category.level, category.description, category.cat1, category.cat2, category.cat3, category.cat4, category.pri_color, category.sec_color, category.last_report AS report_id FROM category, reports_category_layout WHERE category.id = reports_category_layout.catID AND reports_category_layout.site_code = 'las' query2: SELECT DISTINCT category.id, COUNT(forum.id) AS posts, SUM(forum.view) AS views FROM category, forum WHERE category.id = forum.catID AND forum.approved = 'yes' AND forum.site_code = 'las' GROUP BY category.id query3: SELECT forum.catID, forum.title, forum.paragraph, forum.created, users.alias, forum.userID FROM forum, users, forum_cache WHERE forum.catID = forum_cache.catID AND forum.id = forum_cache.last_report AND users.id = forum.userID AND forum.approved = 'yes' Essentially, I am unsure about the syntax to join these properly. I have written a query that simply joins them, but in the case that the forum cache table contains an unapproved forum id, it will simply not return the entire row. what I really need is for query1 and query2 to be left joined on the category id, and for query 3 to be left outer joined on id = catID.

    Read the article

  • SQL Join to only the maximum row puzzle

    - by Billy ONeal
    Given the following example data: Users +--------------------------------------------------+ | ID | First Name | Last Name | Network Identifier | +--------------------------------------------------+ | 1 | Billy | O'Neal | bro4 | +----+------------+-----------+--------------------+ | 2 | John | Skeet | jsk1 | +----+------------+-----------+--------------------+ Hardware +----+-------------------+---------------+ | ID | Hardware Name | Serial Number | +----+-------------------+---------------+ | 1 | Latitude E6500 | 5555555 | +----+-------------------+---------------+ | 2 | Latitude E6200 | 2222222 | +----+-------------------+---------------+ HardwareAssignments +---------+-------------+-------------+ | User ID | Hardware ID | Assigned On | +---------+-------------+-------------+ | 1 | 1 | April 1 | +---------+-------------+-------------+ | 1 | 2 | April 10 | +---------+-------------+-------------+ | 2 | 2 | April 1 | +---------+-------------+-------------+ | 2 | 1 | April 11 | +---------+-------------+-------------+ I'd like to write a SQL query which would give the following result: +--------------------+------------+-----------+----------------+---------------+-------------+ | Network Identifier | First Name | Last Name | Hardware Name | Serial Number | Assigned On | +--------------------+------------+-----------+----------------+---------------+-------------+ | bro4 | Billy | O'Neal | Latitude E6200 | 2222222 | April 10 | +--------------------+------------+-----------+----------------+---------------+-------------+ | jsk1 | John | Skeet | Latitude E6500 | 5555555 | April 11 | +--------------------+------------+-----------+----------------+---------------+-------------+ My trouble is that the maximum "Assigned On" date for each user needs to be selected for each individual user and used for the actual join ... Is there a clever way accomplish this in SQL?

    Read the article

  • Mysql : get data from 2 tables (need help)

    - by quangtruong1985
    Assume that I have 2 tables : members and orders (Mysql) Members : id | name 1 | Lee 2 | brad Orders : id | member_id | status (1: paid, 2: unpaid) | total 1 | 1 | 1 | 1000000 2 | 1 | 1 | 1500000 3 | 1 | 2 | 1300000 4 | 2 | 1 | 3000000 5 | 2 | 2 | 3500000 6 | 2 | 2 | 3300000 I have a sql query : SELECT m.name, COUNT(o.id) as number_of_order, SUM(o.total) as total2 FROM orders o LEFT JOIN members m ON o.member_id=m.id GROUP BY o.member_id which give me this: name | number_of_order | total2 Lee | 3 | 3800000 brad | 3 | 9800000 All that I want is something like this : name | number_of_order | total2 | Paid Unpaid | Paid Unpaid ------------------------------------------------ Lee | 3 | 3800000 | 2 1 | 2500000 1300000 ------------------------------------------------ brad | 3 | 9800000 | 1 2 | 3000000 6800000 ------------------------------------------------ How to make a query that can give me that result? Thanks for your time!

    Read the article

  • Linq to Entity Left Outer Join

    - by radman
    Hi All, I have an Entity model with Invoices, AffiliateCommissions and AffiliateCommissionPayments. Invoice to AffiliateCommission is a one to many, AffiliateCommission to AffiliateCommissionPayment is also a one to many I am trying to make a query that will return All Invoices that HAVE a commission but not necessarily have a related commissionPayment. I want to show the invoices with commissions whether they have a commission payment or not. Query looks something like: using (var context = new MyEntitities()) { var invoices = from i in context.Invoices from ac in i.AffiliateCommissions join acp in context.AffiliateCommissionPayments on ac.affiliateCommissionID equals acp.AffiliateCommission.affiliateCommissionID where ac.Affiliate.affiliateID == affiliateID select new { companyName = i.User.companyName, userName = i.User.fullName, email = i.User.emailAddress, invoiceEndDate = i.invoicedUntilDate, invoiceNumber = i.invoiceNumber, invoiceAmount = i.netAmount, commissionAmount = ac.amount, datePaid = acp.paymentDate, checkNumber = acp.checkNumber }; return invoices.ToList(); } This query above only returns items with an AffiliateCommissionPayment.

    Read the article

  • MySql left join on several regs

    - by egidiocs
    Hi there! I have this table1 idproduct(PK) | date_to_go 1 2010-01-18 2 2010-02-01 3 2010-02-21 4 2010-02-03 and this other table2 that controls date_to_go updates id | idproduct(FK) | prev_date_to_go | date_to_go | update_date 1 1 2010-01-01 2010-01-05 2009-12-01 2 1 2010-01-05 2010-01-10 2009-12-20 3 1 2010-01-10 2010-01-18 2009-12-20 4 3 2010-01-20 2010-02-03 2010-01-05 So, in this example, for table1.idproduct #1 2010-01-18 is the actual date_to_go and 2010-01-01 (table2.prev_date_to_go, first reg) is the original date_to_go . using this query select v.idproduct, v.date_to_go, p.prev_date_to_go original_date_to_go from table1 v left join produto_datas p on p.idproduto = v.idproduto group by (v.idproduto) order by v.idproduto can I assume that original_date_to_go will be the first related reg of table2? idproduct | date_to_go | original_date_to_go 1 2010-01-18 2010-01-01 2 2010-02-01 NULL 3 2010-02-21 2010-01-20 4 2010-02-03 NULL

    Read the article

  • Customizing Hibernate Criteria - Adding conditions to a left join

    - by Douglas Ferguson
    I need to be able to do the following: Select * from Table1 left join Table2 on id1 = id2 AND i1 = ? Hibernate criteria doesn't allow be to specify the i1 = ? part. The existing code is using hibernate criteria and it would be a huge refactor to swap out for HQL Does anybody have any tips how I could implement this differently or any way to override the Hibernate Criteria? I'm not opposed to cracking open hibernate and modifying, but when I began to dig it, there seems to be layers upon layers of abstractions. I never found the the point where SQL is actually generated...

    Read the article

  • Dealing w/ Sqlite Join results in a cursor

    - by Bill
    I have a one-many relationship in my local Sqlite db. Pretty basic stuff. When I do my left outer join I get back results that look like this: the resulting cursor has multiple rows that look like this: A1.id | A1.column1 | A1.column2 | B1.a_id_fk | B1.column1 | B1.column2 A1.id | A1.column1 | A1.column2 | B2.a_id_fk | B2.column1 | B2.column2 and so on... Is there a standard practice or method of dealing with results like this ? Clearly there is only A1, but it has many B-n relationships. I am coming close to using multiple queries instead of the "relational db way". Hopefully I am just not aware of the better way to do things. I intend to expose this query via a content provider and I would hate for all of the consumers to have to write the same aggregation logic.

    Read the article

  • mySQL Left Join on multiple tables

    - by Jarrod
    Hi I'm really struggling with this query. I have 4 tables (http://oberto.co.nz/db-sql.png): Invoice_Payement, Invoice, Client and Calendar. I'm trying to create a report by summing up the 'paid_amount' col, in Invoice_Payment, by month/year. The query needs to include all months, even those with no data There query needs the condition (Invoice table): registered_id = [id] I have tried with the below query, which works, but falls short when 'paid_date' does not have any records for a month. The outcome is that month does not show in the results I added a Calendar table to resolved this but not sure how to left join to it. SELECT MONTHNAME(Invoice_Payments.date_paid) as month, SUM(Invoice_Payments.paid_amount) AS total FROM Invoice, Client, Invoice_Payments WHERE Client.registered_id = 1 AND Client.id = Invoice.client_id And Invoice.id = Invoice_Payments.invoice_id AND date_paid IS NOT NULL GROUP BY YEAR(Invoice_Payments.date_paid), MONTH(Invoice_Payments.date_paid) Please see the above link for a basic ERD diagram of my scenario. Thanks for reading. I've posted this Q before but I think I worded it badly.

    Read the article

  • Doctrine Join DQL

    - by koko
    I's like to do a join between 2 tables on a specific ID. At the moment, I have this DQL: $q = Doctrine_Query::create() ->select('e.*, i.itemName, i.itemtypeId') ->from('Model_EventItem e') ->leftJoin('Model_Item i ON e.itemId = i.itemId') ->where('e.eventitemId = ?', $event->eventId) ->orderBy('i.itemName ASC'); The result is empty, although my eventId has a value ... Can you help me please? I there somewhere a tutorial on DQL-joins? I don't get it right with the help of the Doctrine documentation. Thanks! PS I have doctrine working in combination with Zend Framework.

    Read the article

  • SQL query for getting count on same table using left outer join

    - by Sasi
    Hi all, I have a table from which i need to get the count grouped on two columns. the table has two columns one datetime column and another one is success value(-1,1,0) What i am looking for is something like this... count of success value for each month month----success-----count 11------- -1 ------- 50 11------- 1 --------- 50 11------- 0 ------- 50 12------- -1 ------- 50 12------- 1 ------- 50 12------- 0 ------- 50 if there is no success value for a month then the count should be null or zero. I have tried with left outer join as well but of no use it gives the count incorrectly. Thanks in advance Sasi

    Read the article

  • mysql filtering result using left outer join

    - by user288178
    my query: SELECT content.*, activity_log.content_id FROM content LEFT JOIN activity_log ON content.id = activity_log.content_id AND sess_id = '$sess_id' WHERE activity_log.content_id IS NULL AND visibility = $visibility AND content.reported < ".REPORTED_LIMIT." AND content.file_ready = 1 LIMIT 1 The purpose of that query is to get 1 row from the content table that has not been viewed by the user (identified by session_id), but it still returns contents that have been viewed. What is wrong? ( I have checked the table making sure that the content_ids are there) Note: I think this is more efficient than using subqueries, thoughts?

    Read the article

  • Left Join not returning all rows

    - by DisgruntledGoat
    I have this query in MySQL: SELECT pr.*, pr7.value AS `room_price_high` FROM `jos_hp_properties` pr LEFT OUTER JOIN `jos_hp_properties2` pr7 ON pr7.property=pr.id WHERE pr7.field=23 The jos_hp_properties table has 27 rows but the query only returns one. Based on this question I think it may be because of the WHERE clause. The jos_hp_properties2 table has fields id, property, field, value, where field is a foreign key to a third table (which I don't need to get data from). Is there a way to select all the rows from the first table, including the value from table #2 where the field is 23 (or NULL if there is no field 23)?

    Read the article

  • Rails has_and_belongs_to_many join across 3 tables

    - by Birdman
    I have a three models: listing, category, and site. There is a many to many relationship between listing and site and there is a many to many relationship between listing and category. A listing thus belongs to one or more sites and one or more categories (a listing can appear on multiple sites and multiple categories). Given a site id and a category id, I want a set of all the listings. I understand how to get the listings from a site id (listings = site.listings) and how to get the listings from a category id (listings = category.listings), but how do I get the triple join of the set of listings from both a site id and category id? The sql would look something like (sit_id and cat_id are input): select l.name from listings l, categories_listings cl, listings_sites, ls where cl.category_id = cat_id and ls.site_id = sit_id and cl.listing_id = l.id and ls.listing_id = l.id Thanks in advance

    Read the article

  • join two oracle queries

    - by coder247
    I've to query from two tables and want one result.. how can i join these two queries? First query is querying from two tables and the second one is only from one. select pt.id,pt.promorow,pt.promocolumn,pt.type,pt.image,pt.style,pt.quota_allowed,ptc.text,pq.quota_left from promotables pt,promogroups pg ,promotablecontents ptc ,promoquotas pq where pt.id_promogroup = 1 and ptc.country ='049' and ptc.id_promotable = pt.id and pt.id_promogroup = pg.id and pq.id_promotable = pt.id order by pt.promorow,pt.promocolumn select pt.id,pt.promorow,pt.promocolumn,pt.type,pt.image,pt.style,pt.quota_allowed from promotables pt where pt.type='heading'

    Read the article

  • How do I replace NOT EXISTS with JOIN?

    - by YelizavetaYR
    I've got the following query: select distinct a.id, a.name from Employee a join Dependencies b on a.id = b.eid where not exists ( select * from Dependencies d where b.id = d.id and d.name = 'Apple' ) and exists ( select * from Dependencies c where b.id = c.id and c.name = 'Orange' ); I have two tables, relatively simple. The first Employee has an id column and a name column The second table Dependencies has 3 column, an id, an eid (employee id to link) and names (apple, orange etc). the data looks like this Employee table looks like this id | name ----------- 1 | Pat 2 | Tom 3 | Rob 4 | Sam Dependencies id | eid | Name -------------------- 1 | 1 | Orange 2 | 1 | Apple 3 | 2 | Strawberry 4 | 2 | Apple 5 | 3 | Orange 6 | 3 | Banana As you can see Pat has both Orange and Apple and he needs to be excluded and it has to be via joins and i can't seem to get it to work. Ultimately the data should only return Rob

    Read the article

  • mysql join default value

    - by andy
    I've been trying to use the IsNull() function to ensure that there is a value for a field. $result = mysql_query(" SELECT crawled.id,IsNull(sranking.score,0) as Score,crawled.url,crawled.title,crawled.blurb FROM crawled LEFT JOIN sranking ON crawled.id = sranking.sid WHERE crawled.body LIKE '%".$term."%' ORDER BY Score DESC LIMIT " . $start . "," . $c . " ") or die(mysql_error()); But I get the error message:Incorrect parameter count in the call to native function 'IsNull' Anybody have any ideas? I'm pretty new to mySQL.

    Read the article

  • MySQL Query Join Table Selecting Highest Date Value

    - by ALHUI
    Here is the query that I run SELECT cl.cl_id, cc_rego, cc_model, cl_dateIn, cl_dateOut FROM courtesycar cc LEFT JOIN courtesyloan cl ON cc.cc_id = cl.cc_id Results: 1 NXI955 Prado 2013-10-24 11:48:38 NULL 2 RJI603 Avalon 2013-10-24 11:48:42 2013-10-24 11:54:18 3 RJI603 Avalon 2013-10-24 12:01:40 NULL The results that I wanted are to group by the cc_rego values and print the most recent cl_dateIn value. (Only Display Rows 1,3) Ive tried to use MAX on the date and group by clause, but it combines rows, 2 & 3 together showing both the highest value of dateIn and dateOut. Any help will be appreciated.

    Read the article

  • MySQL - Join as zero if record Not IN

    - by Zurahn
    To explain by example, take two tables, A and B Table A id foo 1 x 2 y 3 z Table B id aid bar 1 3 50 2 1 100 An example join SELECT foo, bar FROM a, b WHERE a.id = b.aid; Garners a result of foo bar z 50 x 100 What I would like to do is get all values of foo and for any instances where there isn't a corresponding bar value, return 0 for that column. My best guess was something along the lines of SELECT foo, bar AS br FROM a, b WHERE a.id = b.aid OR a.id NOT IN (SELECT aid FROM b); But that returns duplicates and non-zero values for bar. Possible?

    Read the article

  • MySQL, join one item to multiple items in a single query

    - by bschaeffer
    Say I've got two tables. One contains a list of items, and one contains pictures of the items. Something like this: Items +----+---------+ | id | name | +----+---------+ | 1 | oranges | | 2 | apples | +----+---------+ Pictures +----+---------+ | id | item_id | +----+---------+ | 1 | 1 | | 2 | 2 | | 3 | 2 | +----+---------+ My question is this: is it possible in a single query to select item 2 from Items and join all the rows in the Pictures table whose item_id references that same id? I am sure I've tried almost everything I can think of, but nothing is coming to mind. I apologize if the answer to this is simple, but It's just escaping me right now.

    Read the article

< Previous Page | 8 9 10 11 12 13 14 15 16 17 18 19  | Next Page >