I found this query that can combine two tables with different number of rows and without any related fields:
SELECT a.*, b.* FROM table1 a, table2 b;
Now my problem is that in my case I cannot change the value of the FROM.
Example I have this Query:
SELECT * FROM table1;
I cannot change that to:
SELECT * FROM table2;
At most, I can only add statements after FROM table_name LEFT JOIN somedefaulttable ON somedefaulttable2.id = somedefaulttable .id_c. (I forgot to add this important detail, I can only add custom queries after this line, I cannot remove this predetermined LEFT JOIN as this is system generated) The reason for this is because I am restricted by the CRM I am using, and I cannot edit the default select value but I can add additional custom queries after the said statement.
Now what my main problem here is that I need to combine a different table in my Query and I cannot use join as they don't have any common values and their number of rows are also different, I also tried using JOIN before to no avail, and thus decided to try using a different approach such as merging the two tables.
This is the link to my previous question where I was using JOIN to achieve my goal of combining the tables. In this link, you can see that I want to combine Table A and Table 4 but I cannot do so with JOIN as they have a different number of rows and that I am limited in changing my current Query to fit the posted answer.
Table A.
id | name | deleted | amount | due_date | status
1 | a | 0 | 10 | 2016-07-18 | Unpaid
2 | b | 0 | 20 | 2016-07-19 | Unpaid
3 | c | 0 | 15 | 2016-07-18 | Unpaid
Table B
id | name | due_date | status
1 | a | | Unpaid
2 | b | | Unpaid
3 | c | | Unpaid
4 | d | 2016-07-19 | Unpaid
Table C
id | table_d_id | table_a_id
1 | 1 | 1
2 | 2 | 2
3 | 3 | 3
Table D
id |
1
2
3
Try this for (inner) joining two tables without a JOIN Statement.
SELECT a.x, a.y, b.x, b.y FROM table1 a, table2 b WHERE a.x = b.x
What I do not understand is, how different numbers of rows affect the requested solution.
i have created a link of one to many between the tables
with your example and number of rows you inserted you should try this:
SELECT TableC.*
FROM TableB, TableD INNER JOIN (TableA INNER JOIN TableC ON TableA.ID = TableC.table_a_id) ON TableD.ID = TableC.table_d_id;
this will bring all the records of table C
Related
I have two tables:
1st: reasons
id | title
---------------------------------
1 | Customer didn't like it
2 | Needs improving
3 | Wrong format
2nd: projects
id | title | rejected
------------------------------------
1 | Priject 1 | Null
2 | Priject 2 | 1
3 | Priject 3 | 1
4 | Priject 4 | Null
5 | Priject 5 | 2
I need to display Reasons.Title and number of project rejected for that reason. I've managed to join those tables together, with this code
SELECT reasons.title as title, count(*) as num
FROM reasons
LEFT JOIN reasons on projects.rejected = reasons.id
WHERE projects.rejectedIS NOT NULL
GROUP BY projects.rejected
Now I need to add percentage, so my final table looks like this
title | num | percentage
--------------------------------------------------
Customer didn't like it | 2 | 66,6
Needs improving | 1 | 33,3
The format of percentage is of course not important.
I would like to get this done with MySql, so I do not need to use two queries and extra PHP, but if there is another solution, other from MySql, I'm open to suggestions
You can do this by getting the total in the FROM clause:
SELECT r.title as title, count(*) as num,
COUNT(*) / pp.cnt as ratio
FROM reasons r JOIN
projects p
ON p.rejected = r.id CROSS JOIN
(SELECT COUNT(*) as cnt FROM projects p WHERE rejects IS NOT NULL) pp
GROUP BY r.title, pp.cnt;
Notes:
This fixes the table names, so the query has a projects table.
This removes the WHERE because it is not needed.
This changes the LEFT JOIN to an inner join.
I'm tyring to fetch those records (users) whose following each other. I have following table.
id | follower | following
--+-----------+----------
1 | a | c
2 | b | a
3 | a | g
4 | g | b
5 | c | a
What I'm trying ?
I'm trying simple like operator for the task.
SELECT * FROM `table` WHERE follower LIKE '%a%' OR following LIKE '%a%'
Result
id | follower | following
--+-----------+----------
1 | a | c
2 | b | a
3 | a | g
4 | c | a
Above query is working fine as per the logic and like operator, but i wanted to fetch those records whose follow each other. Means in above result i need only first and last record, because a and c are following each other.
I need
id | follower | following
--+-----------+----------
1 | a | c
2 | c | a
Can anyone guide me how can i fetch this kind of record, I would like appreciate if someone help me. Thank You
Try these.
SELECT * FROM table WHERE (follower='a' AND following='c') OR (follower='c' AND following='a')
Your goal is to retrieve both users who are following each other right? In these query, it will select whether a is following c or c is following a.
select t1.*
from thetable t1
join thetable t2
on t1.follower = t2.following and t2.follower = t1.following
demo
UPDATE:
If you interest of user 'a'
select t1.*
from t t1
join t t2
on t1.follower = t2.following and t2.follower = t1.following
where 'a' in (t1.follower,t1.following,t2.follower,t2.following)
demo
I have two different tables with a similar column in both. And i need to query for all rows in table A but must exempt specific rows in table A if those rows exist in table B.
Example:
Table A
---------------------------------
item_id | item_name | price
---------------------------------
1 | apple | 100
---------------------------------
2 | banana | 150
---------------------------------
3 | ginger | 120
---------------------------------
4 | pear | 150
---------------------------------
5 | berry | 120
---------------------------------
Table B
---------------------------------
item_id | item_owner |
---------------------------------
1 | Ben |
---------------------------------
2 | Damian |
---------------------------------
3 | Greg |
---------------------------------
Based on the example above, I need to run a query to fetch all the rows in table A if item_id does not exist in table B.
The result of this query should fetch only 2 rows which are:
---------------------------------
4 | pear | 150
---------------------------------
5 | berry | 120
---------------------------------
Would ber glad to get help with this...Thank!
use LEFT JOIN
SELECT a.*
FROM tableA a
LEFT JOIN tableB b
ON a.item_id = b.item_id
WHERE b.item_id IS NULL
SQLFiddle Demo
For faster performance, you must define an INDEX on column item_id on both tables to prevent server to perform FULL TABLE SCAN.
To fully gain knowledge about joins, kindly visit the link below:
Visual Representation of SQL Joins
SELECT *
FROM TableA
WHERE item_id NOT IN(SELECT item_id FROM tableb);
Demo.
Try this::
Select
A.*
from
A LEFT JOIN B on (A.item_id=B.item_id)
where B.item_id is null
Try this query...
SELECT A.*
FROM TableA A FULL OUTER JOIN TableB
ON A.Item_id = B.Item_ID
WHERE B.Item_ID IS NULL
I am new to the world of mysql and I'm having some trouble getting the data I need from a database.
The 2 tables I have are...
Results
ID | TITLE | LOTS OF OTHER DATA |
1 | res1 | |
2 | res2 | |
3 | res3 | |
4 | res4 | |
5 | res5 | |
Categories
ID | RESULT_ID | CATEGORY NAME |
1 | 1 | purchase |
2 | 1 | single_family |
3 | 1 | conventional |
4 | 2 | usda |
5 | 3 | somecategory |
I'm trying to create a query that will select results that belong to all of the categories provided in the query. For example a query for purchase & single_family & conventional in this example would return the first result in the results table.
Does that make sense? Is there a query that will do this or is this more of a problem with my database structure?
Thanks a lot!
Try something like this:
SELECT * FROM Results r
INNER JOIN Categories c on r.ID = c.RESULT_ID
WHERE c.name in ('purchase', 'single_family', 'conventional')
GROUP BY r.ID
HAVING COUNT(c.ID) = 3
The basic select with join will get you three rows only for result 1.
Edit: To make sure your code won't break if you change your database you should always select the fields you want explicitly: SELECT r.ID, .. FROM ..
So you're basically doing a simple join with all the category table for all categories where the category name is one of the names in the list. Try to run the 3 first lines manually to see the result you get.
Next you group by the result id. This means that you are aggregating all the rows sharing the same result id into one row. The last line means that we are filtering the aggregated columns that are aggregated by 3 rows. That means that you will only return results that have 3 matching categories.
So the only problem with this approach is if you have duplicate result_id, categoryname in your Categories table.
i have a form that has a multiple select drop down. a user can select more than one options in the select. the name of the select is array[]; using php I call implode(",",$array)
in mysql db, it stores the field as a text in this format "places"= "new york, toronto, london" when i want to display these fields i explode the commas.
I am trying to run a report to display the places. here is my select:
"select * from mytable where db.places .. userSelectedPlaces"
how can i check toronto in lists of "places" that user selected? note "places" in the db might be either just "toronto" or it might be comma separated lists of places like "ny, toronto, london, paris, etc".
If it is possible, you would be much better off using another table to hold the places that the user has selected. Call it SelectedPlaces with columns:
mytable_id - To join back to the table in your query
place - EG: "Toronto"
Then you can run a simple query to figure out if Toronto has been selected:
SELECT *
FROM mytable m
INNER JOIN SelectedPlaces sp ON sp.mytable_id = m.id
WHERE sp.place = 'Toronto'
If I understand you correctly, your database design is just wrong. Try reading about it more. Generally, in good design you should not have lists of values as one field in database and you should introduce new table for it.
But if you want to do it this way, you can use strcmp function.
If i understood correctly, this should work:
WHERE DB.PLACES LIKE '%TORONTO%'
but as other users said, its not a nice thing to have denormalized tables.
To directly answer your question, your query needs to look something like this
SELECT *
FROM mytable
WHERE places LIKE( '%toronto%' )
But, be aware, that LIKE() is slow.
To indirectly answer your question, your database schema is all wrong. That is not the right way to do a M:N (many-to-many) relationship.
Imagine instead you had this
mytable place mytable_place
+------------+ +----------+----------+ +------------+----------+
| mytable_id | | place_id | name | | mytable_id | place_id |
+------------+ +----------+----------+ +------------+----------+
| 1 | | 1 | new york | | 1 | 1 |
| 2 | | 2 | toronto | | 1 | 2 |
| 3 | | 3 | london | | 1 | 3 |
+------------+ +----------+----------+ | 2 | 2 |
| 3 | 1 |
| 3 | 3 |
+------------+----------+
The table mytable_places is what's called a lookup table (or, xref/cross-reference table, or correlation table). Its only job is to keep track of which mytable records have which place records, and vice versa.
From this example we can see that The 1st mytable record has all 3 places, the 2nd has only toronto, and the 3rd has new york and london.
This opens you up too all sorts of queries that would be difficult, expensive, or impossible with your current design.
Want to know how many mytable records have toronto? No problem
SELECT COUNT(*)
FROM mytable_place x
LEFT JOIN place p
ON p.place_id = x.place_id
WHERE p.name = 'toronto';
How about the number of mytable records per place, sorted?
SELECT p.name
, COUNT(*) as `count`
FROM mytable_place x
LEFT JOIN place p
ON p.place_id = x.place_id
GROUP BY p.place_id
ORDER BY `count` DESC, p.name ASC
And these are going to be much faster than any query using LIKE since they can use indexes on columns such as place.name.