samedi 27 juin 2015

SQL query who returns the number of straight victory

I have the following table :

match(id, userId1, userId2, userIdWinner, date)

I would like to get the number of straight victory for a specific user.

If you have some ideas to do that...

Thx :)

rails join query between two tables with similar field

I have 3 models

class Company < ActiveRecord::Base
  has_many : CompanyAccount
  has_many : CompanyContact
end

class CompanyContact < ActiveRecord::Base
  belongs_to : Company
end

class CompanyAccount < ActiveRecord::Base
  belongs_to : Company
end

As both the CompanyAccount and CompanyContact models belong to the Company model, they have a similar "company_id" field. I have retrieved some Accounts through a query:

@CompanyAccounts = CompanyAccount.where.not(balance:nil)

Now, using the common company_id field I am trying to retrieve all the data from my CompanyContacts table that belong to the same Company associated with the CompanyAccounts I queried above (in other words, I am trying to get the rows which have the same company_id). I have made several attempts using "joins" but everything failed so far. Could anyone give me what would be the appropriate syntax in this context? Thanks.

How can I get my C# and MySql to return more than one entry from the database?

This is an excerpt from my code. Everything works the way it's supposed to, but I'm trying to add a feature which allows the program to return more than one row from the database - that is to say, more than one result.

If I search for "silver", I get Silverado; if I search "silver l", I get Silver Linings Playbook.

What I've been trying to get it to do is to search for "silver" and get Silverado AND Silver Linings Playbook, but my loops haven't fixed the issue and some help would be...helpful.

            movieBox.Items.Clear();
            try
            {
            db_connection.Open();
            sql_command = new MySqlCommand("select * from mov_db.movies where title like '%" + searchBox.Text + "%'", db_connection);
            sql_reader = sql_command.ExecuteReader();
            if (sql_reader.Read())
                {
                movieBox.Items.Add(sql_reader.GetString("title"));
                movieBox.Items.Add(sql_reader.GetString("year"));
                movieBox.Items.Add(sql_reader.GetString("genre"));
                movieBox.Items.Add(sql_reader.GetString("rating"));
                }
            else
                {
                MessageBox.Show("Sorry, but that title is unavailable.");
                }
            }

MariaDB and 1064 error

I get the following error when I execute the code below:

ERROR 1064 (42000) at line 21: You have an error in your SQL syntax; chech the manual that corresponds to your MariaDB server version for the right syntax to use near @LINE_TERMINATION@

Here is the code that I use to create table:

CREATE TABLE SRDEF (
    RT  VARCHAR (3) BINARY NOT NULL,
    UI  CHAR (4) BINARY NOT NULL,
    STY_RL  VARCHAR (41) BINARY NOT NULL,
    STN_RTN VARCHAR (14) BINARY NOT NULL,
    EX  VARCHAR (185) BINARY
) CHARACTER SET utf8;

And here is the code I use to populate table:

load data local infile 'SRDEF' into table SRDEF fields terminated by '|' 
ESCAPED BY '' lines terminated by @LINE_TERMINATION@
(@rt, @ui, @sty_rl, @stn_rtn, @ex)
SET RT = @rt,
UI = @ui,
STY_RL = @sty_rl,
STN_RTN = @stn_rtn,
EX = NULLIF(@ex,'');

Any advice is greatly appreciated.

Entity Framework - code first - Too many navigation properties

I have two tables created with Entity Framework code first that I would like some help with..!

Tables

  1. AccountLinks, 3 composite keys
  2. Guest, 3 composite foreign keys (?)

Table overview

enter image description here

SQL overview

enter image description here

As you can see I have ALOT of navigation properties in my database which I dont want.

Code for AccountLink

public class AccountLink
    {
        public AccountLink()
        {
            AccountLinkPermissionAccountLinkID = new HashSet<AccountLinkPermission>();
            AccountLinkPermissionAccountOwnerID = new HashSet<AccountLinkPermission>();
            AccountLinkPermissionGuestID = new HashSet<AccountLinkPermission>();
        }

        public AccountLink(int accountOwnerID, int guestID, DateTime dateCreated, DateTime dateStart, DateTime dateExpires)
        {
            AccountLinkPermissionAccountLinkID = new HashSet<AccountLinkPermission>();
            AccountLinkPermissionAccountOwnerID = new HashSet<AccountLinkPermission>();
            AccountLinkPermissionGuestID = new HashSet<AccountLinkPermission>();
            this.AccountOwnerID = accountOwnerID;
            this.GuestID = guestID;
            this.DateCreated = dateCreated;
            this.DateStart = dateStart;
            this.DateExpires = dateExpires;
        }

        [Key, Column(Order = 0)]
        public int AccountLinkID { get; set; }
        [Key, Column(Order = 1)]
        public int AccountOwnerID { get; set; }
        [Key, Column(Order = 2)]
        public int GuestID { get; set; }

        public DateTime DateCreated { get; set; }
        public DateTime DateStart { get; set; }
        public DateTime DateExpires { get; set; }

        [ForeignKey("AccountOwnerID")]
        public virtual AccountOwner AccountOwner { get; set; }

        [ForeignKey("GuestID")]
        public virtual Guest Guest { get; set; }

        public virtual ICollection<AccountLinkPermission> AccountLinkPermissionAccountLinkID { get; set; }
        public virtual ICollection<AccountLinkPermission> AccountLinkPermissionAccountOwnerID { get; set; }
        public virtual ICollection<AccountLinkPermission> AccountLinkPermissionGuestID { get; set; }
    }

Code for AccountLinkPermissions

public class AccountLinkPermission
    {
        public AccountLinkPermission()
        {

        }

        public AccountLinkPermission(int accountLinkID, int accountOwnerID, int guestID, int permissionID)
        {
            this.AccountLinkID = accountLinkID;
            this.AccountOwnerID = accountOwnerID;
            this.GuestID = guestID;
            this.PermissionID = permissionID;
        }

        [Key, Column(Order = 0)]
        public int AccountLinkID { get; set; }
        [Key, Column(Order = 1)]
        public int AccountOwnerID { get; set; }
        [Key, Column(Order = 2)]
        public int GuestID { get; set; }
        [Key, Column(Order = 3)]
        public int PermissionID { get; set; }

        [InverseProperty("AccountLinkPermissionAccountLinkID")]
        public virtual AccountLink AccountLink { get; set; }

        [InverseProperty("AccountLinkPermissionAccountOwnerID")]
        public virtual AccountLink AccountLinkAccountOwner { get; set; }

        [InverseProperty("AccountLinkPermissionGuestID")]
        public virtual AccountLink AccountLinkGuest { get; set; }

        [ForeignKey("PermissionID")]
        public virtual Permission Permission { get; set; }
    }

The reason to why I want 3 composite keys is because I want to prevent duplicates.

Why I use InverseProperty instead of ForeignKey

Because I'm using multiple foreign keys linked to the same table, EF is not able to determine by convention which navigation properties belong together. Instead of using the property [ForeignKey] I have to use [InverseProperty] which defines the navigation property on the other end of the relationship.

What I need help with

How do I remove all the navigation properties in my database using code first? I know I messed it up somewhere but that's all I know :)

1 Navigation property in AccountLinks, (User_UserID)

9 Navigtion properties in AccountLinkPermissions

Bonus question

In AccountLink table I have three composite keys, AccountLinkID, AccountOwnerID and GuestID. Is it possible to put auto increment, (identity seed), on AccountLinkID? How would I do that in EF code first?

SELECT column WHERE time_type = 'Break' but only the rows that are after(below) time_type = 'Start'

currently I'm stuck in an issue, hope some good PostgreSQL fellow programmer could give me a hand with it. This is my table...

enter image description here

I would like to SELECT all 'time_elapse' WHERE time_type = 'Break' but only the rows that are after(below) the last(descendent) time_type = 'Start' and sum them up.

So in the table above I would SELECT...

time_elapse           |   time_type  |  time_index
----------------------+--------------+-------------
00-00-00 01:00:00.00  |   Break      |  2.1
00-00-00 01:00:00.00  |   Break      |  2.2

So totalbreak = 00-00-00 02:00:00.000

I know how to convert character varying to timestamp in order to sum them up (please don't bother with that, I'm not looking for help with that, let's image 'time' and 'time_elapse' columns are proper timestamps), I just don't know how would be the syntax to select all possible 'Breaks' and sum them up (lets say the max Breaks between each 'Start' is nine).

I can hardly imagine a way to do that, so I would like to ask for suggestions.

Msg 512, Level 16, State 1, Procedure trg_pricebase, Line 13 Subquery returned more than 1 value

how to solve this kind of probe help me please:

error:

Msg 512, Level 16, State 1, Procedure trg_pricebase, Line 13 Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression. The statement has been terminated.


and my trigger is :

ALTER TRIGGER [dbo].[trg_pricebase]
ON  [dbo].[tbl_model2]        
AFTER UPDATE
AS 
   BEGIN

                DECLARE @price_base NVARCHAR(50) = (SELECT tbl_model2.price_base FROM tbl_model2)
                DECLARE @tipid  int = (SELECT tbl_model2.id FROM tbl_model2)


                INSERT INTO tbl_price_history (tbl_price_history.price_base,tbl_price_history.tipid)
                VALUES (@price_base, @tipid )

    END