I have two tables users and admin which contains same column emp_code.
I have a input field with button. I need to enter emp_code in input field.I need to get the rows from two tables which contains emp_code which i entered.
I need sql query to get the rows from two tables which contains emp_code which i entered.
I think you need all the users from user and admin table which have emp code similar to code given in the input field. Then you have to use "Union" in the sql query (Assuming both tables have the same columns):
SELECT username FROM ADMIN
WHERE EMP_CODE LIKE 'your_emp_code_here'
UNION
SELECT username FROM USERS
WHERE EMP_CODE LIKE 'your_emp_code_here'
It seems like you are new to DB Queries.
Well here is another simplest solution, (without join)
SELECT * FROM USERS, ADMIN
WHERE USERS.EMP_CODE = ADMIN.EMP_CODE
AND USERS.EMP_CODE like 'your_emp_code_here'
Related
SQL query, find the user that does not have bank_account
I have two tables, the first one is table bank_customer, and the other is table user.
The table of bank_customer has many columns, including user_id and bank_account.
in table user, I have column id.
not every user has bank_account. so I want to query, users that do not have a bank_account, but it gets hard.
SELECT * FROM users, bank_customers
WHERE NOT users.id=bank_customers.user_id
so how can I query in SQL with this case?
and how to logic that case? for the next case I can find out by myself.
There are a few ways to go about that, but the simplest way would probably be using IN:
SELECT *
FROM users
WHERE id NOT IN (
SELECT user_id
FROM bank_customers
)
i have 2 tables customers and imap_emails. customer table contain emails of customers.
I am using PHP-IMAP to fetch email from email server and then saving to database table imap_emails.
the imap_emails table has 2 fields from and to and to field contains comma separated values.
i need to fetch emails from first table and then search against to and from on imap_emails.
First i thought about a LIKE condition to search , but i would like to have something like FIND_IN_SET or something other.
How can i achieve this in a better way ? ( For some reasons i cannot use relations on this table )
Please advice.
Example of doing a join based on FIND_IN_SET
SELECT *
FROM imap_emails
INNER JOIN customers
ON FIND_IN_SET(customers.email, imap_emails.to) > 0
You don't say how you want to narrow this down (ie, if an email uses 2 email addresses do you want both emails brought back), so can't add much at this stage.
i have 2 tables customers and imap_emails. customer table contain emails of customers.
I am using PHP-IMAP to fetch email from email server and then saving to database table imap_emails.
the imap_emails table has 2 fields from and to and to field contains comma separated values.
i need to fetch emails from first table and then search against to and from on imap_emails.
First i thought about a LIKE condition to search , but i would like to have something like FIND_IN_SET or something other.
How can i achieve this in a better way ? ( For some reasons i cannot use relations on this table )
Please advice.
Example of doing a join based on FIND_IN_SET
SELECT *
FROM imap_emails
INNER JOIN customers
ON FIND_IN_SET(customers.email, imap_emails.to) > 0
You don't say how you want to narrow this down (ie, if an email uses 2 email addresses do you want both emails brought back), so can't add much at this stage.
My two MySQL tables have different information but one field is common. I am showing you the structure of two tables.
I need to show it in a single report filtering by cust_id.
i.e. Customer Id wise billing and payment report.
I tried...
SELECT * FROM billing_info as a,payment_info as b WHERE a.cust_id='1' AND b.cust_id='1' AND a.cust_id=b.cust_id
but rows are repeating.
Hope I explained this properly. Now what should I do ?
Is it possible in Crystal Report to show two tables data ?
I guess ur table structure may resulting duplicate entries.
in table payment_info instead of using the cust_id (reference of cust table), u should use id of table billing_info so that we will get more precised output for ur query.
U also get payment details agains which bill has been made.
I've been reading through tutorials on Rails' active record model operations. And I'm a little confused on the difference between .select and .group. If I wanted to get all the names of all my users in table User I believe I could do:
myUsers = User.select(:name)
so how would that be different from saying:
myUsers = User.group(:name)
thanks,
Will
The two differ like this:
User.select(:name)
is equivalent to this SQL statement
SELECT name from users;
and
User.group(:name)
is equivalent to
SELECT * from users GROUP BY name;
The difference is that with select(:name) you are taking all rows ordered by id, but only with column name. With group(:name) you are taking all rows and all columns, but ordered by column name.
User.pluck(:name) will be the fastest way to pull all the names from your db.
There is #to_sql method to check what DB query it is building. By looking at the DB query, you can confirm yourself what is going on. Look the below example :-
arup#linux-wzza:~/Rails/tv_sms_voting> rails c
Loading development environment (Rails 4.1.4)
>> Vote.group(:choice).to_sql
=> "SELECT \"votes\".* FROM \"votes\" GROUP BY choice"
>> Vote.select(:choice).to_sql
=> "SELECT \"votes\".\"choice\" FROM \"votes\""
>>
Now it is clear that Vote.select(:choice) is actually, SELECT "votes"."choice" FROM "votes", which means, select choice column from all rows of the table votes.
Vote.group(:choice) is grouping the rows of the votes table, based on the column choice and selecting all columns.
If I wanted to get all the names of all my users in table User.
Better is User.pluck(:name).