mySQL Select from two tables where match or not exists - php

I have a problem setting up a SQL Query, hoping someone can help me.
So here's the task, I have two tables I would like to get with a single query. Not a big problem, unless it comes to WHERE clause matching. I need all entries from Table A, but only matching entries from Table B, however keeping entries from Table A where the linked ID is not existing.
To make clear what I have here is an example structure for the Tables...
TABLE A
ID | VAL1 | VAL2 | VAL3
1 | abc | xyz | 123
2 | abc | xyz | 123
3 | abc | xyz | 123
4 | abc | xyz | 123
TABLE B
ID | A-ID | X1 | X2 | X3 | FLAG
1 | 1 | ab | xy | 98 | 1
2 | 1 | ab | xy | 98 | 1
3 | 1 | ab | xy | 98 | 0
4 | 2 | ab | xy | 98 | 1
5 | 2 | ab | xy | 98 | 0
6 | 4 | ab | xy | 98 | 1
So if use this Query...
SELECT a.*, b.* FROM Table_A AS a LEFT JOIN Table_B AS b ON b.a-id = a.id WHERE b.flag = 0
... I get of course only the entries of A that have the matches in B, which would be ID 1 and 2 in this example, because 3 has no entry in B and 4 only an entry with FLAG 1.
However, in the Result-Array, I would need A3 and A4 as well, with the B Array-Values simply to be empty.
I have currenlty no clue if this can be done easily and in a single Query. I already tried a different approach by changing the query to something like...
SELECT a.*, (SELECT b.* FROM Table_B AS b WHERE b.a-id = a.id) AS array FROM Table_A AS a
... but in this case b.* is not allowed. :(

Thanks Pat for your suggestion, I just found a solution that is working for me in this case, so for anyone who might be interested, I moved the WHERE clause to the ON clause and now I get the result I needed...
SELECT a.*, b.* FROM Table_A AS a LEFT JOIN Table_B AS b ON (b.a-id = a.id AND b.flag = 0)
Need to keep that in mind next time. :)

Related

Select from multiple tables on reference table with same column name MYSQL/PHP

I need some help with MySQL/PHP. Which is the faster execution in code point of view.
I have below tables
1. table_content
--------------------------------
id | section | content_id
--------------------------------
1 | A | 15
2 | B | 25
3 | A | 9
--------------------------------
2. table_a
--------------------------------
id | name | message
--------------------------------
9 | John | Hello Everyone
15 | Smita | Hi
17 | Vinayak | How are you?
--------------------------------
3. table_b
--------------------------------
id | label | description
--------------------------------
1 | David | D1
5 | Alia | D2
25 | Vinay | D3
--------------------------------
I have above table structure. For me table_content is main table. I want below output through MySQL/PHP [As array and section as key].
Output
------------------------------------------------
id | section | name | message
------------------------------------------------
1 | A | Smita | Hi
2 | B | Vinay | D3
3 | A | John | Hello Everyone
------------------------------------------------
I have tried with SWITCH case. But not getting exact output.
Which is the better performance and fast execution? with MySQL or PHP. I have thousands of data like this.
Please help me to solve this problem.
Should be acheved via a JOIN.
Also ensure you have configured appropriater indexing, otherwise it will perform badly when you get to a large volume of data.
As i posted in my comment you need to Join the both tables, which must be UNION
SELECT tc.`id`, tc. `section` ,t1.`message`
FROM table_content tc JOIN (SELECT `id`, `name`, `message` FROM table_a UNION SELECT `id`, `label`, `description` FROM table_b) t1
ON tc.`content_id` = t1.`id`
id | section | message
-: | :------ | :-------------
1 | A | Hi
2 | B | D3
3 | A | Hello Everyone
db<>fiddle here

Get all the data from a table and sometimes relate that table with another

My question is: Can I do this?
I try many things, some of them are:
Search 1
Search 2
Search 3
Search 4
I need all the info form Table A, and sometimes I need to join this table with Table B. My problem is that when I join both tables, if an specific parameter in Table A is not in Table B just give me the records when the specific parameter are, but I want all the records.
Table A
|--------------------------------|
| Table A |
|-------------------|------------|
| id_table_A | name | id_table_B |
|------------|------|------------|
| 1 | Joe | 1 |
|------------|------|------------|
| 2 | Ben | |
|------------|------|------------|
| 3 | Lya | |
|------------|------|------------|
| 4 | luis | 2 |
|------------|------|------------|
Table B
|----------------------|
| Table B |
|----------------------|
| id_table_B | Elements|
|------------|---------|
| 1 | Car |
|------------|---------|
| 2 | Byke |
|------------|---------|
| 3 | Moto |
|------------|---------|
What I want to show in my View is this:
|------------|------|------------|
| id_table_A | name | Elements |
|------------|------|------------|
| 1 | Joe | Car |
|------------|------|------------|
| 2 | Ben | |
|------------|------|------------|
| 3 | Lya | |
|------------|------|------------|
| 4 | luis | Byke |
|------------|------|------------|
My model
In my model this is what I tried:
"SELECT * FROM table_A, table_B where table_A.id_table_B = table_B.id_table_B"
But this query only show me data 1 and 4.
This can be done or not?
Thanks in advance.
You can use left join
SELECT *
FROM table_A
left join table_B on table_A.id_table_B = table_B.id_table_B
Left join is used when the keys between tables may not always match. In this case, the left join retrieves the correct match where it's possible and the values become NULL when not possible.
SQL LEFT JOIN Documentation
You need an explicit LEFT JOIN as opposed to the implicit INNER JOIN that is used when you simply list the tables like that. https://www.w3schools.com/sql/sql_join_left.asp

How do I get the number of rows from a table and insert into the same table in MySQL?

I have a problem, I would like to write a MySql query to achieve the result below:
Id | C1 | C2 | Score | Q | CCount | RF |
1 | A | B | 0.25 | 40 | 4 |
2 | A | B | 0.60 | 40 | 4 |
3 | A | C | 0.10 | 20 | 2 |
4 | A | B | 0.90 | 40 | 4 |
5 | A | C | 0.30 | 20 | 2 |
6 | A | B | 0.70 | 40 | 4 |
The CCount column is the total number of rows per combination ie AB, AC etc..
In the table the above ABs have a total count of 4 rows while the ACs have a total count of 2 rows.
Should I use 2 tables? The Main table and a count table and then, insert count result back into the main table using group by etc.. If so, how would I go about it?
Is there another way of solving my problem?
This part below, I can do:
I am doing this because I would like to calculate RF (Relative Frequency) of each row.
RF = Score * Q/CCount
I'm pretty sure you are doing this wrong (as I cant see the whole problem).
Just tested this now, you can do this in query.
select count(CONCAT(C1,C2)) as count, CONCAT(C1,C2) as name from data group by C1,C2
output is
count combo
4 AB
2 AC
From here you should be able to get the RF as I have got the totals.
To make things simple you can now do a 2nd query running through the rows and calculating the score.
Its possible that you could do a subquery. But that is getting really tricky.
John.

Run While Loop Through All Distinct Values of A Column

This is an example MYSQL result
+----+---+
| A | B |
+----+---+
| 1 | 1 |
| 1 | 2 |
| 2 | 3 |
| 2 | 4 |
| 3 | 5 |
+----+---+
I would like to run through every distinct in Column A and do something utilizing the values in Column B.
Let's say A has userids and B has foods. I would like to grab all the foods that user 1 likes and then shoot an email to 1, then grab all the foods that user 2 likes and email to her, and so forth. Would appreciate any suggestions.
If you want comma separated values, you can use GROUP_CONCAT
SELECT A, GROUP_CONCAT(DISTINCT B) foodList
FROM tableName
GROUP BY A
SQLFiddle Demo
Other Link
GROUP BY clause

query to imlement block list

I have two tables a and b as follows to implement a simple block list where users can block other users.....
Table A
+------------+--------------+------+
| Name | phone |userid|
+------------+--------------+------+
| Mr Sasi | 01225 708225 | 1 |
| Miss Brown | 01225 899360 | 2 |
| Mr Black | 01380 724040 | 3 |
+------------+--------------+------+
Table B
+------------+--------------+
| blockedbyid| blockedid |
+------------+--------------+
| 1 | 2 |
| 2 | 3 |
| 1 | 3 |
+------------+--------------+
"blockedbyid" is id of user who has blocked the user in "blockedid".
I need to join the two tables and fetch all records from table A such that the result has all users who are not blocked by a particular user [ie blockedbyid='XXX'].. Can you guys give the SQL query so that i can fetch the records as a recordset??? I dont want to fetch two different rowsets and compare it in php....
Something like this should work
Parameter :USERID
SELECT * FROM TABLEA WHERE userid NOT IN (SELECT blockedid FROM TABLEB WHERE blockedbyid = :USERID)
Using join
SELECT u.* FROM TABLEB b, TABLEA u WHERE b.blockedbyid = 'XXX' AND b.blockedid = NULL
It may work like that, give it a try.
Roadie57 solutions seems better though.

Categories