can i use select statements for different tables in a single query
SELECT *
FROM users
WHERE first_name LIKE "%'.$search_string.'%"
OR last_name LIKE "%'.$search_string.'%"
thats from the table ' users' , i want to do the same search on another table called sellers. sellers have the same fields first_name, last_name
Yes, you can
Select column1,column2,column3,column4
From table1,table 2
Where Conditions
If you want to do join between tables, check if theirs PK and FK are equals
Eg:
Select table1Id,table2Id,column3,column4
From table1 t1,table2 t2
Where t1=t2
You can search like this
select * from shirts where find_in_set('1','2,3,4,5,1')
Related
I know that I can use the OR SQL operator on the same column of a table like the following:
SELECT * FROM users WHERE last_name = 'Peter' OR last_name = 'Smith';
But the situation I have right now is that I'm trying to use the OR operator on two different tables (different column names). Is that possible? How can I achieve that in an SQL query?
And yes there is a foreign key column that links one table to the primary key column of the other table.
Thanks in advance for any help.
If the fields in the different tables have the same name, you can distinguish them with tablename.fieldname, if the tables have the same name (in different schemas), you can further qualify the names with schemaname.tablename.fieldname.
Of course, all tables referenced in the WHERE and SELECT clauses should be included in the FROM clause.
Note: If a table is aliased in the FROM, the alias should be used instead of the table name.
You cannot display (or use in WHERE conditions, etc...) fields from a table that not included in the FROM; however, you can use subqueries on those tables.
Examples:
...
FROM table1 AS t1
WHERE t1.field1 = somevalue
OR EXISTS (
SELECT *
FROM table2 AS t2
WHERE t2.somefield = someothervalue
)
...
or
SELECT t1.field1
, (SELECT t2.somefield FROM table2 AS t2 WHERE t2.anotherfield = somevalue LIMIT 1) AS t2Val
FROM table1 AS t1
...
Yes , it is possible
SELECT users.* FROM users,countries WHERE users.last_name = 'Peter' OR countries.name = 'mexico' AND users.idCountry=countries.id;
Is there a way in MySQL to do a single SQL statement that returns the selected rows along with the count of the result rows?
I can do this:
SELECT COUNT(*) FROM BigTable WHERE firstname LIKE 'a%';
Which gives me a single result row with the count (37,781). I can get the actual row data like this:
SELECT firstname FROM BigTable WHERE firstname LIKE 'a%';
which displays the actual 37,781 rows. But when I try to combine them, like this:
SELECT firstname, COUNT(*) FROM BigTable WHERE firstname LIKE 'a%';
I get a single row with the first row that matches the query, and the total count of records that matches the query.
What I'd like to see is two columns with 37,781 rows. The first column should contain the first name for each row and the second column should contain the number '37,781' for every row. Is there a way to write the query to accomplish this?
You can use a CROSS JOIN. The subquery will get the count for all firstnames and then it will include this value in each row:
SELECT firstname, d.total
FROM BigTable
CROSS JOIN
(
SELECT COUNT(*) total
FROM BigTable
WHERE firstname LIKE 'a%'
) d
WHERE firstname LIKE 'a%';
See SQL Fiddle with Demo
You can join with a subquery:
SELECT firstname, ct
FROM BigTable
JOIN (SELECT COUNT(*) ct
FROM BigTable
WHERE firstname LIKE 'a%') x ON (1 = 1)
WHERE firstname LIKE 'a%'
The cross join is not the efficient way, the better way is to use an inline SELECT like the following structure:
SELECT firstname,
(select count(*) from BigTable where firstname like 'a%') as count
from BigTable
where firstname like 'a%'
I tested both approaches with 50k records in database, and this approach is almost 2x faster.
I feel like this used to be the case for older versions of MySQL, but this isn't working in my tests.
But according to the manual, http://dev.mysql.com/doc/refman/5.1/en/group-by-functions.html#function_count Given that COUNT(*) is a group by function, and naturally groups all of the rows together, when a GROUP BY statement is not attached, I can only see the solution to this being either multiple statements, or a sub-query. I would suggest running the 2 queries separately, if you can, but if that isn't possible, Try:
SELECT firstname,
total
FROM BigTable,
( SELECT COUNT(*) AS total
FROM BigTable ) AS dummy
WHERE firstname LIKE 'a%';
I have two tables, both having column a device_id column that I want to count. For the purposes of demonstration, the schema looks like:
Table 1: 'id', 'save_val', 'device_id_major'
Table 2: 'id', 'save_val', 'location', 'device_id_team'
Table 1 could have many of the same 'device_id_major'.
I basically want to get the unique device_id's from both tables, then from that result set, get the count of unique device_id's (the same device_id can appear in both tables).
Is this possible in one query?
select distinct aa.device_id, count(*)
from(select distinct device_id from table1
union all
select distinct device_id from table2) as aa
group by device_id
order by device_id
Or something like... As I don't have the schema to hand, I can't fully validate it.
SELECT count(DISTINCT aa.id)
FROM (SELECT DISTINCT major_id AS id FROM `major`
UNION ALL
SELECT DISTINCT team_id AS id FROM `team`)
AS aa
This seems to do the trick.
You could use a query that takes the UNION of both tables, then SELECT the unique values.
To be specific I am using MySql.I have many tables in my database, like employee, student, employer etc etc. There are more than 20 such tables. And in each table, I have a column called 'username' and 'age'. Now I want a query that will give me the all the ages when i give a particular username='alex'.
You probably should have a single table (say, Users) which has a column indicating what type of person that user is (employee, student, employer, etc.). However, if you must query across multiple tables of this sort, use UNION:
SELECT age FROM employee WHERE username = 'alex' UNION ALL
SELECT age FROM student WHERE username = 'alex' UNION ALL
SELECT age FROM employer WHERE username = 'alex' -- etc.
It looks like poor design of the database and if you can change the database structure, that's probably the way to go. Otherwise, you could try this:
(SELECT username, age
FROM table1)
UNION
(SELECT username, age
FROM table2)
UNION
(SELECT username, age
FROM table3)
...
Query to get and tablenames having a particular column
select table_name from information_schema.columns
where table_name in (
select table_name from information_schema.tables
where table_schema='databaseName'
) and
column_name='username';
from the above query you will be all tablenames which have the field username then i PHP you can build query to get values from all the tables;
There are two tables: users1 and users2. They both have name column. I need select all users from users1 that are absent in users2 table.
I can only select all users and iterate them by PHP, checking every in second table.
Is there a way to do it by SQL?
SELECT `users1`.* FROM `users1` LEFT JOIN `users2` USING (`name`)
WHERE `users2`.`name` IS NULL
For maximum performance, be sure you have an index defined on name in both tables.
This can also be done with a subquery (as others have pointed out), but a join will execute much faster.
Maybe you can try to write a sub query like
SELECT *
FROM Users1
WHERE Username NOT IN
(SELECT Username FROm Users2)
Hope this could help
SELECT * FROM users1 WHERE name NOT IN(SELECT name FROM users2)
Depending on your RMDB and data in this tables, you might want to turn all names to lower case:
SELECT * FROM users1 WHERE LOWER(name) NOT IN(SELECT LOWER(name) FROM users2)
select * from users1 where name not in (select name from users2);