How to skip match data when select - php

I have two tables in my database-
First Table Named area:
id name
1 a
2 b
3 c
Second Table Named data:
id name area_id end_date
1 a1 1,2 2014-07-14 09:50:00
I want to select rows from first table only if that id is not present in area_id set of my second table and whose end_date is greater than or equal to current date.
my query (not working) -
select a.* from area as a where a.id NOT IN (
SELECT find_in_Set(area_id) FROM data
WHERE date(end_date)>=CURRENT_DATE()
)

If I interpret your question right and the result of this query should be
id | name
---------
3 | c
then you could get your result with a little modification. You forgot the needle that should be found in the set:
SELECT a.*
FROM area as a
WHERE a.id NOT IN (
SELECT FIND_IN_SET(a.id, area_id) FROM data -- you forgot the needle
WHERE date(end_date) >= CURRENT_DATE()
)
But I would recommend to follow the good advice of juergen d and change the table structure, if you can.

Related

Query to fetch data from two different tables

I have two tables. Table A and B.
I have a log-in form where I will get the username,password, assign, assign2 of the user from table A and fetch data depending on the user's assign1 and assign2 from table B.
How my query would look like?
Looking for answers. Thankyou so much.
Table A -THIS IS THE TABLE FOR LOG-IN FORM
==========================================
username | password | assign1 | assign2 |
------------------------------------------
SANDRA | SANTOS | 1 | 1 | //Values
--------------------------------------------
Table B -
=======================================
name | assign1 | assign 2 |
------------------------------
DADA | 1 | 1 | //this will be displayed
------------------------------
gorg | 2 | 2 |
//this must not be displayed since the user assign1 and assign2 who logged in did not match to this
You are using $sql variable in
$result = $con->query($sql);
It should be $queryagain.
Your question is not clear, but if you want to fetch data from Table B which depends on assign1 and assign2 you can do it like this:
$queryagain = mysqli_query("SELECT * FROM tableB INNER JOIN tableA ON tableB.name=tableA.username WHERE tableA.assign1 = tableB.assign1 AND tableA.assign2 = tableB.assign2");
When you use JOIN it's best practice to JOIN by primary key (id). If tableB.name=tableA.username are not columns with same value you can join by other columns, like assign1, or assign2:
assign1:
$queryagain = mysqli_query("SELECT * FROM tableB INNER JOIN tableA ON tableB.assign1=tableA.assign1 WHERE tableA.assign2 = tableB.assign2");
assign2:
$queryagain = mysqli_query("SELECT * FROM tableB INNER JOIN tableA ON tableB.assign2=tableA.assign2 WHERE tableA.assign1 = tableB.assign1");
Notice: Where clause is not necessay, you can edit WHERE clause depends of the result you want.
I think your are very very new to database programing. First of all I recomend you to read https://www.w3schools.com/sql/sql_join.asp this page and work on examples using try it yourself part.
Join Types:
INNER JOIN: Returns records that have matching values in both tables. That means if assign field is filled in Table A and the value is included in table B then this type of join should match the rows.
LEFT JOIN: Return all records from the left table, and the matched records from the right table. That means all rows in Table A will match either the value is included in table B or not.
RIGHT JOIN: Return all records from the right table, and the matched records from the left table. That means all rows in Table B will match either the value is included in table A or not.
FULL JOIN: Return all records when there is a match in either left or right table. All rows in table A and table B will be included in te result set either they match or not.
Your SQL query may look like:
$sql = "SELECT * FROM table_a a INNER JOIN table_b b on a.assign1 = b.assign1 INNER JOIN table_b b2 on a.assign2 = b2.assign2";
execution part of the sql can be different depending your database engine or other libraries.
I think this thread also helps you: Join two mysql tables with php

count repeated value in table field

how i can count the repeated value in any field of mysql db's table.
Example:
id name
1 aaa
2 aaa
3 ttt
4 ccc
5 ttt
6 ccc
7 aaa
8 zzz
How i can get how many times value is repeated in table like.
aaa =3 times in table
ttt =2 times in table
ccc =2 times in table
zzz =1 times in table
I think it possible with count of the mysql but how to use it i dont know any one can help ?please answer ma question thanks in adv.
You need to group by your name column and then you can use an aggregate function like count() on that group
select name, count(id)
from your_table
group by name
Write below query to get count of each name
select name, count(name)
from your_table
group by name
OR
to get count of specific name
select name, count(name)
from your_table
where name = "aaa"
group by name

how can i count specific field multi rows of mysql using php

how can I count Mysql Field Rows?
For example I have table called student_attendance where we have 4 fields: absent, present, holiday, leave.
There is list of 10+ students, each student value will go to its own field, like if for one student we selected absent, 1 value will go to absent field and if someone select present, 1 value will go to present field.
Now what i want is
Each time attendance take, new row insert in student_attendance table for the student.
So if there are 10 Rows in student_attendance table, how can i + all of them
Like if there are 10 rows of present field, 3 rows are empty and 7 rows have 1 value, how can i count it so that the total value of specific field 1+1+1+1+1+1+1 can comes in php so it show 7 ?
SELECT SUM(present) AS presence_days
FROM student_attendance
This will result:
+---------------+
| presence_days |
+---------------+
| 7 |
| |
+---------------+
Example SQL (Demo):
select count(*) from table_name where present_field=1;
Try this:
SELECT SUM(absent), SUM(present), SUM(holiday), SUM(leave)
FROM student_attendance
GROUP BY sudentId;

PHP MySQL Select ID from one table and information from another table

I have two tables, one table is called queuelist and the other is call info. In the queuelist table it just lists different IDs. I am trying to get the 'clientID' from that table and match it with the 'ID' in the other table that contains all of the info and display it back on the page. Here is how the tables look:
Table - queuelist
ID | clientID
-------------
1 | 589
2 | 254
3 | 486
Table - info
ID | Name | Phone
--------------------
256 | Bob | 5551231234
486 | Jack | 5551231234
589 | Jill | 5551231234
This is what they call joining tables, you should use a query like this:
SELECT i.ID, i.Name, i.Phone FROM `queuelist` AS q
LEFT JOIN `info` AS i ON (
q.clientID = i.ID
);
I'm using aliases for shorter notation in the above query (queuelist becomes q and info becomes i) and then set the join condition (the bit between the ON()) to be the clientID from the queuelist table should match the ID in the info table.
Also see http://dev.mysql.com/doc/refman/5.0/en/join.html for more details.
You need to use an inner join
select * from queuelist as ql inner join info as i on ql.clientID = i.ID
Though you might want to replace * with specific field names e.g
select ql.clientID, i.fieldname FROM....
Well, I see no difficulty in this using a JOIN.
SELECT * FROM queuelist JOIN info ON clientID = info.ID WHERE queuelist.ID = 2
"Where" would be another option.
SELECT Name, Phone FROM queuelist,info WHERE clientID = ID
Assuming you want only name and phone

Compare two tables and update second table

There are two tables table1 and table2
table1 has got two columns name and rank
table2 has got only one column name
names in table2 are almost listed in table1
I want compare two table and pull rank info from table1 and update/alter table2 with rank
table1
name | rank
-------------
john | 2
mathews| 5
keyn | 4
emly | 25
yancy | 8
stewart| 9
kim | 12
chris | 19
table2
name
-------
john
mathews
keyn
emly
yancy
stewart
I want update/insert rank details to table2 from table1
thats it and sorry for the confusion
Seem like you want to do something like this:
update table2,table1 set table2.rank=table1.rank where table2.name=table1.name
This will update the second table with ranks from the first table where the names are equal.
Then put auto increament field of table one in table2. and after this apply left join using these id and pull the info
refer this link
http://www.wellho.net/solutions/mysql-left-joins-to-link-three-or-more-tables.html

Categories